robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
23.0 KiB733 linesraw
1// Compositor: merge floating grids with the main grid for display in
2// TUI and non-multigrid UIs.
3//
4// Layer-based compositing: https://en.wikipedia.org/wiki/Digital_compositing
5
6#include <assert.h>
7#include <inttypes.h>
8#include <limits.h>
9#include <stdbool.h>
10#include <stdlib.h>
11#include <string.h>
12#include <uv.h>
13
14#include "klib/kvec.h"
15#include "nvim/api/private/defs.h"
16#include "nvim/ascii_defs.h"
17#include "nvim/buffer_defs.h"
18#include "nvim/globals.h"
19#include "nvim/grid.h"
20#include "nvim/highlight.h"
21#include "nvim/highlight_defs.h"
22#include "nvim/highlight_group.h"
23#include "nvim/log.h"
24#include "nvim/macros_defs.h"
25#include "nvim/memory.h"
26#include "nvim/message.h"
27#include "nvim/option_vars.h"
28#include "nvim/os/time.h"
29#include "nvim/types_defs.h"
30#include "nvim/ui.h"
31#include "nvim/ui_compositor.h"
32
33#include "ui_compositor.c.generated.h"
34
35static int composed_uis = 0;
36static kvec_t(ScreenGrid *) layers = KV_INITIAL_VALUE;
37
38static size_t bufsize = 0;
39static schar_T *linebuf;
40static sattr_T *attrbuf;
41
42#ifndef NDEBUG
43static int chk_width = 0, chk_height = 0;
44#endif
45
46static ScreenGrid *curgrid;
47
48static bool valid_screen = true;
49static int msg_current_row = INT_MAX;
50static bool msg_was_scrolled = false;
51
52static int msg_sep_row = -1;
53static schar_T msg_sep_char = schar_from_ascii(' ');
54
55static int dbghl_normal, dbghl_clear, dbghl_composed, dbghl_recompose;
56
57void ui_comp_init(void)
58{
59  kv_push(layers, &default_grid);
60  curgrid = &default_grid;
61}
62
63#ifdef EXITFREE
64void ui_comp_free_all_mem(void)
65{
66  kv_destroy(layers);
67  xfree(linebuf);
68  xfree(attrbuf);
69}
70#endif
71
72void ui_comp_syn_init(void)
73{
74  dbghl_normal = syn_check_group(S_LEN("RedrawDebugNormal"));
75  dbghl_clear = syn_check_group(S_LEN("RedrawDebugClear"));
76  dbghl_composed = syn_check_group(S_LEN("RedrawDebugComposed"));
77  dbghl_recompose = syn_check_group(S_LEN("RedrawDebugRecompose"));
78}
79
80void ui_comp_attach(RemoteUI *ui)
81{
82  composed_uis++;
83  ui->composed = true;
84}
85
86void ui_comp_detach(RemoteUI *ui)
87{
88  composed_uis--;
89  if (composed_uis == 0) {
90    XFREE_CLEAR(linebuf);
91    XFREE_CLEAR(attrbuf);
92    bufsize = 0;
93  }
94  ui->composed = false;
95}
96
97bool ui_comp_should_draw(void)
98{
99  return composed_uis != 0 && valid_screen;
100}
101
102/// Raises or lowers the layer, syncing comp_index with zindex.
103///
104/// This function adjusts the position of a layer in the layers array
105/// based on its zindex, either raising or lowering it.
106///
107/// @param[in]  layer_idx  Index of the layer to be raised or lowered.
108/// @param[in]  raise      Raise the layer if true, else lower it.
109void ui_comp_layers_adjust(size_t layer_idx, bool raise)
110{
111  size_t size = layers.size;
112  ScreenGrid *layer = layers.items[layer_idx];
113
114  if (raise) {
115    while (layer_idx < size - 1 && layer->zindex > layers.items[layer_idx + 1]->zindex) {
116      layers.items[layer_idx] = layers.items[layer_idx + 1];
117      layers.items[layer_idx]->comp_index = layer_idx;
118      layers.items[layer_idx]->pending_comp_index_update = true;
119      layer_idx++;
120    }
121  } else {
122    while (layer_idx > 0 && layer->zindex < layers.items[layer_idx - 1]->zindex) {
123      layers.items[layer_idx] = layers.items[layer_idx - 1];
124      layers.items[layer_idx]->comp_index = layer_idx;
125      layers.items[layer_idx]->pending_comp_index_update = true;
126      layer_idx--;
127    }
128  }
129  layers.items[layer_idx] = layer;
130  layer->comp_index = layer_idx;
131  layer->pending_comp_index_update = true;
132}
133
134/// Places `grid` at (col,row) position with (width * height) size.
135/// Adds `grid` as the top layer if it is a new layer.
136///
137/// TODO(bfredl): later on the compositor should just use win_float_pos events,
138/// though that will require slight event order adjustment: emit the win_pos
139/// events in the beginning of update_screen(), rather than in ui_flush()
140bool ui_comp_put_grid(ScreenGrid *grid, int row, int col, int height, int width, bool valid,
141                      bool on_top)
142{
143  bool moved;
144  grid->pending_comp_index_update = true;
145
146  if (grid->comp_index != 0) {
147    moved = (row != grid->comp_row) || (col != grid->comp_col);
148    if (ui_comp_should_draw()) {
149      // Redraw the area covered by the old position, and is not covered
150      // by the new position. Disable the grid so that compose_area() will not
151      // use it.
152      grid->comp_disabled = true;
153      compose_area(grid->comp_row, row,
154                   grid->comp_col, grid->comp_col + grid->comp_width);
155      if (grid->comp_col < col) {
156        compose_area(MAX(row, grid->comp_row),
157                     MIN(row + height, grid->comp_row + grid->comp_height),
158                     grid->comp_col, col);
159      }
160      if (col + width < grid->comp_col + grid->comp_width) {
161        compose_area(MAX(row, grid->comp_row),
162                     MIN(row + height, grid->comp_row + grid->comp_height),
163                     col + width, grid->comp_col + grid->comp_width);
164      }
165      compose_area(row + height, grid->comp_row + grid->comp_height,
166                   grid->comp_col, grid->comp_col + grid->comp_width);
167      grid->comp_disabled = false;
168    }
169    grid->comp_row = row;
170    grid->comp_col = col;
171  } else {
172    moved = true;
173#ifndef NDEBUG
174    for (size_t i = 0; i < kv_size(layers); i++) {
175      if (kv_A(layers, i) == grid) {
176        abort();
177      }
178    }
179#endif
180
181    size_t insert_at = kv_size(layers);
182    while (insert_at > 0 && kv_A(layers, insert_at - 1)->zindex > grid->zindex) {
183      insert_at--;
184    }
185
186    if (curwin && kv_A(layers, insert_at - 1) == &curwin->w_grid_alloc
187        && kv_A(layers, insert_at - 1)->zindex == grid->zindex
188        && !on_top) {
189      insert_at--;
190    }
191    // not found: new grid
192    kv_pushp(layers);
193    for (size_t i = kv_size(layers) - 1; i > insert_at; i--) {
194      kv_A(layers, i) = kv_A(layers, i - 1);
195      kv_A(layers, i)->comp_index = i;
196      kv_A(layers, i)->pending_comp_index_update = true;
197    }
198    kv_A(layers, insert_at) = grid;
199
200    grid->comp_row = row;
201    grid->comp_col = col;
202    grid->comp_index = insert_at;
203    grid->pending_comp_index_update = true;
204  }
205
206  grid->comp_height = height;
207  grid->comp_width = width;
208  if (moved && valid && ui_comp_should_draw()) {
209    compose_area(grid->comp_row, grid->comp_row + grid->rows,
210                 grid->comp_col, grid->comp_col + grid->cols);
211  }
212  return moved;
213}
214
215void ui_comp_remove_grid(ScreenGrid *grid)
216{
217  assert(grid != &default_grid);
218  if (grid->comp_index == 0) {
219    // grid wasn't present
220    return;
221  }
222
223  if (curgrid == grid) {
224    curgrid = &default_grid;
225  }
226
227  for (size_t i = grid->comp_index; i < kv_size(layers) - 1; i++) {
228    kv_A(layers, i) = kv_A(layers, i + 1);
229    kv_A(layers, i)->comp_index = i;
230    kv_A(layers, i)->pending_comp_index_update = true;
231  }
232  (void)kv_pop(layers);
233  grid->comp_index = 0;
234  grid->pending_comp_index_update = true;
235
236  // recompose the area under the grid
237  // inefficient when being overlapped: only draw up to grid->comp_index
238  ui_comp_compose_grid(grid);
239}
240
241bool ui_comp_set_grid(handle_T handle)
242{
243  if (curgrid->handle == handle) {
244    return true;
245  }
246  ScreenGrid *grid = NULL;
247  for (size_t i = 0; i < kv_size(layers); i++) {
248    if (kv_A(layers, i)->handle == handle) {
249      grid = kv_A(layers, i);
250      break;
251    }
252  }
253  if (grid != NULL) {
254    curgrid = grid;
255    return true;
256  }
257  return false;
258}
259
260void ui_comp_raise_grid(ScreenGrid *grid, size_t new_index)
261{
262  size_t old_index = grid->comp_index;
263  for (size_t i = old_index; i < new_index; i++) {
264    kv_A(layers, i) = kv_A(layers, i + 1);
265    kv_A(layers, i)->comp_index = i;
266    kv_A(layers, i)->pending_comp_index_update = true;
267  }
268  kv_A(layers, new_index) = grid;
269  grid->comp_index = new_index;
270  grid->pending_comp_index_update = true;
271  for (size_t i = old_index; i < new_index; i++) {
272    ScreenGrid *grid2 = kv_A(layers, i);
273    int startcol = MAX(grid->comp_col, grid2->comp_col);
274    int endcol = MIN(grid->comp_col + grid->cols,
275                     grid2->comp_col + grid2->cols);
276    compose_area(MAX(grid->comp_row, grid2->comp_row),
277                 MIN(grid->comp_row + grid->rows, grid2->comp_row + grid2->rows),
278                 startcol, endcol);
279  }
280}
281
282void ui_comp_grid_cursor_goto(Integer grid_handle, Integer r, Integer c)
283{
284  if (!ui_comp_set_grid((int)grid_handle)) {
285    return;
286  }
287  int cursor_row = curgrid->comp_row + (int)r;
288  int cursor_col = curgrid->comp_col + (int)c;
289
290  // TODO(bfredl): maybe not the best time to do this, for efficiency we
291  // should configure all grids before entering win_update()
292  if (curgrid != &default_grid) {
293    size_t new_index = kv_size(layers) - 1;
294
295    while (new_index > 1 && kv_A(layers, new_index)->zindex > curgrid->zindex) {
296      new_index--;
297    }
298
299    if (curgrid->comp_index < new_index) {
300      ui_comp_raise_grid(curgrid, new_index);
301    }
302  }
303
304  if (cursor_col >= default_grid.cols || cursor_row >= default_grid.rows) {
305    // TODO(bfredl): this happens with 'writedelay', refactor?
306    // abort();
307    return;
308  }
309  ui_composed_call_grid_cursor_goto(1, cursor_row, cursor_col);
310}
311
312ScreenGrid *ui_comp_mouse_focus(int row, int col)
313{
314  for (ssize_t i = (ssize_t)kv_size(layers) - 1; i > 0; i--) {
315    ScreenGrid *grid = kv_A(layers, i);
316    if (grid->mouse_enabled
317        && row >= grid->comp_row && row < grid->comp_row + grid->rows
318        && col >= grid->comp_col && col < grid->comp_col + grid->cols) {
319      return grid;
320    }
321  }
322  if (ui_has(kUIMultigrid)) {
323    FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
324      ScreenGrid *grid = &wp->w_grid_alloc;
325      if (grid->mouse_enabled && row >= wp->w_winrow && row < wp->w_winrow + grid->rows
326          && col >= wp->w_wincol && col < wp->w_wincol + grid->cols) {
327        return grid;
328      }
329    }
330  }
331  return NULL;
332}
333
334/// Compute which grid is on top at supplied screen coordinates
335ScreenGrid *ui_comp_get_grid_at_coord(int row, int col)
336{
337  for (ssize_t i = (ssize_t)kv_size(layers) - 1; i > 0; i--) {
338    ScreenGrid *grid = kv_A(layers, i);
339    if (row >= grid->comp_row && row < grid->comp_row + grid->rows
340        && col >= grid->comp_col && col < grid->comp_col + grid->cols) {
341      return grid;
342    }
343  }
344
345  FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
346    ScreenGrid *grid = &wp->w_grid_alloc;
347    if (row >= grid->comp_row && row < grid->comp_row + grid->rows
348        && col >= grid->comp_col && col < grid->comp_col + grid->cols
349        && !wp->w_config.hide) {
350      return grid;
351    }
352  }
353  return &default_grid;
354}
355
356/// Baseline implementation. This is always correct, but we can sometimes
357/// do something more efficient (where efficiency means smaller deltas to
358/// the downstream UI.)
359static void compose_line(Integer row, Integer startcol, Integer endcol, LineFlags flags)
360{
361  // If rightleft is set, startcol may be -1. In such cases, the assertions
362  // will fail because no overlap is found. Adjust startcol to prevent it.
363  startcol = MAX(startcol, 0);
364  // in case we start on the right half of a double-width char, we need to
365  // check the left half. But skip it in output if it wasn't doublewidth.
366  int skipstart = 0;
367  int skipend = 0;
368  if (startcol > 0 && (flags & kLineFlagInvalid)) {
369    startcol--;
370    skipstart = 1;
371  }
372  if (endcol < default_grid.cols && (flags & kLineFlagInvalid)) {
373    endcol++;
374    skipend = 1;
375  }
376
377  int col = (int)startcol;
378  ScreenGrid *grid = NULL;
379  schar_T *bg_line = &default_grid.chars[default_grid.line_offset[row]
380                                         + (size_t)startcol];
381  sattr_T *bg_attrs = &default_grid.attrs[default_grid.line_offset[row]
382                                          + (size_t)startcol];
383
384  while (col < endcol) {
385    int until = 0;
386    for (size_t i = 0; i < kv_size(layers); i++) {
387      ScreenGrid *g = kv_A(layers, i);
388      // compose_line may have been called after a shrinking operation but
389      // before the resize has actually been applied. Therefore, we need to
390      // first check to see if any grids have pending updates to width/height,
391      // to ensure that we don't accidentally put any characters into `linebuf`
392      // that have been invalidated.
393      int grid_width = MIN(g->cols, g->comp_width);
394      int grid_height = MIN(g->rows, g->comp_height);
395      if (g->comp_row > row || row >= g->comp_row + grid_height
396          || g->comp_disabled) {
397        continue;
398      }
399      if (g->comp_col <= col && col < g->comp_col + grid_width) {
400        grid = g;
401        until = g->comp_col + grid_width;
402      } else if (g->comp_col > col) {
403        until = MIN(until, g->comp_col);
404      }
405    }
406    until = MIN(until, (int)endcol);
407
408    assert(grid != NULL);
409    assert(until > col);
410    assert(until <= default_grid.cols);
411    size_t n = (size_t)(until - col);
412
413    if (row == msg_sep_row && grid->comp_index <= msg_grid.comp_index) {
414      // TODO(bfredl): when we implement borders around floating windows, then
415      // msgsep can just be a border "around" the message grid.
416      grid = &msg_grid;
417      sattr_T msg_sep_attr = (sattr_T)HL_ATTR(HLF_MSGSEP);
418      for (int i = col; i < until; i++) {
419        linebuf[i - startcol] = msg_sep_char;
420        attrbuf[i - startcol] = msg_sep_attr;
421      }
422    } else {
423      size_t off = grid->line_offset[row - grid->comp_row]
424                   + (size_t)(col - grid->comp_col);
425      memcpy(linebuf + (col - startcol), grid->chars + off, n * sizeof(*linebuf));
426      memcpy(attrbuf + (col - startcol), grid->attrs + off, n * sizeof(*attrbuf));
427      if (grid->comp_col + grid->cols > until
428          && grid->chars[off + n] == NUL) {
429        linebuf[until - 1 - startcol] = schar_from_ascii(' ');
430        if (col == startcol && n == 1) {
431          skipstart = 0;
432        }
433      }
434    }
435
436    // 'pumblend' and 'winblend'
437    if (grid->blending) {
438      int width;
439      for (int i = col - (int)startcol; i < until - startcol; i += width) {
440        width = 1;
441        // negative space
442        bool thru = (linebuf[i] == schar_from_ascii(' ')
443                     || linebuf[i] == schar_from_char(L'\u2800')) && bg_line[i] != NUL;
444        if (i + 1 < endcol - startcol && bg_line[i + 1] == NUL) {
445          width = 2;
446          thru &= (linebuf[i + 1] == schar_from_ascii(' ')
447                   || linebuf[i + 1] == schar_from_char(L'\u2800'));
448        }
449        attrbuf[i] = (sattr_T)hl_blend_attrs(bg_attrs[i], attrbuf[i], &thru);
450        if (width == 2) {
451          attrbuf[i + 1] = (sattr_T)hl_blend_attrs(bg_attrs[i + 1],
452                                                   attrbuf[i + 1], &thru);
453        }
454        if (thru) {
455          memcpy(linebuf + i, bg_line + i, (size_t)width * sizeof(linebuf[i]));
456        }
457      }
458    }
459
460    // Tricky: if overlap caused a doublewidth char to get cut-off, must
461    // replace the visible half with a space.
462    if (linebuf[col - startcol] == NUL) {
463      linebuf[col - startcol] = schar_from_ascii(' ');
464      if (col == endcol - 1) {
465        skipend = 0;
466      }
467    } else if (col == startcol && n > 1 && linebuf[1] == NUL) {
468      skipstart = 0;
469    }
470
471    col = until;
472  }
473  if (linebuf[endcol - startcol - 1] == NUL) {
474    skipend = 0;
475  }
476
477  assert(endcol <= chk_width);
478  assert(row < chk_height);
479
480  if (!(grid && (grid == &default_grid || (grid->comp_col == 0 && grid->cols == Columns)))) {
481    flags = flags & ~kLineFlagWrap;
482  }
483
484  for (int i = skipstart; i < (endcol - skipend) - startcol; i++) {
485    if (attrbuf[i] < 0) {
486      if (rdb_flags & kOptRdbFlagInvalid) {
487        abort();
488      } else {
489        attrbuf[i] = 0;
490      }
491    }
492  }
493  ui_composed_call_raw_line(1, row, startcol + skipstart,
494                            endcol - skipend, endcol - skipend, 0, flags,
495                            (const schar_T *)linebuf + skipstart,
496                            (const sattr_T *)attrbuf + skipstart);
497}
498
499static void compose_debug(Integer startrow, Integer endrow, Integer startcol, Integer endcol,
500                          int syn_id, bool delay)
501{
502  if (!(rdb_flags & kOptRdbFlagCompositor) || startcol >= endcol) {
503    return;
504  }
505
506  endrow = MIN(endrow, default_grid.rows);
507  endcol = MIN(endcol, default_grid.cols);
508  int attr = syn_id2attr(syn_id);
509
510  if (delay) {
511    debug_delay(endrow - startrow);
512  }
513
514  for (int row = (int)startrow; row < endrow; row++) {
515    ui_composed_call_raw_line(1, row, startcol, startcol, endcol, attr, false,
516                              (const schar_T *)linebuf,
517                              (const sattr_T *)attrbuf);
518  }
519
520  if (delay) {
521    debug_delay(endrow - startrow);
522  }
523}
524
525static void debug_delay(Integer lines)
526{
527  ui_call_flush();
528  uint64_t wd = (uint64_t)llabs(p_wd);
529  uint64_t factor = (uint64_t)MAX(MIN(lines, 5), 1);
530  os_sleep(factor * wd);
531}
532
533static void compose_area(Integer startrow, Integer endrow, Integer startcol, Integer endcol)
534{
535  compose_debug(startrow, endrow, startcol, endcol, dbghl_recompose, true);
536  endrow = MIN(endrow, default_grid.rows);
537  endcol = MIN(endcol, default_grid.cols);
538  if (endcol <= startcol) {
539    return;
540  }
541  for (int r = (int)startrow; r < endrow; r++) {
542    compose_line(r, startcol, endcol, kLineFlagInvalid);
543  }
544}
545
546/// compose the area under the grid.
547///
548/// This is needed when some option affecting composition is changed,
549/// such as 'pumblend' for popupmenu grid.
550void ui_comp_compose_grid(ScreenGrid *grid)
551{
552  if (ui_comp_should_draw()) {
553    compose_area(grid->comp_row, grid->comp_row + grid->rows,
554                 grid->comp_col, grid->comp_col + grid->cols);
555  }
556}
557
558void ui_comp_raw_line(Integer grid, Integer row, Integer startcol, Integer endcol, Integer clearcol,
559                      Integer clearattr, LineFlags flags, const schar_T *chunk,
560                      const sattr_T *attrs)
561{
562  if (!ui_comp_should_draw() || !ui_comp_set_grid((int)grid)) {
563    return;
564  }
565
566  row += curgrid->comp_row;
567  startcol += curgrid->comp_col;
568  endcol += curgrid->comp_col;
569  clearcol += curgrid->comp_col;
570  if (curgrid != &default_grid) {
571    flags = flags & ~kLineFlagWrap;
572  }
573
574  assert(endcol <= clearcol);
575
576  // TODO(bfredl): this should not really be necessary. But on some condition
577  // when resizing nvim, a window will be attempted to be drawn on the older
578  // and possibly larger global screen size.
579  if (row >= default_grid.rows) {
580    DLOG("compositor: invalid row %" PRId64 " on grid %" PRId64, row, grid);
581    return;
582  }
583  if (clearcol > default_grid.cols) {
584    DLOG("compositor: invalid last column %" PRId64 " on grid %" PRId64,
585         clearcol, grid);
586    if (startcol >= default_grid.cols) {
587      return;
588    }
589    clearcol = default_grid.cols;
590    endcol = MIN(endcol, clearcol);
591  }
592
593  bool covered = curgrid_covered_above((int)row, (int)row, (int)startcol, (int)clearcol);
594  // TODO(bfredl): eventually should just fix compose_line to respect clearing
595  // and optimize it for uncovered lines.
596  if (flags & kLineFlagInvalid || covered || curgrid->blending) {
597    compose_debug(row, row + 1, startcol, clearcol, dbghl_composed, true);
598    compose_line(row, startcol, clearcol, flags);
599  } else {
600    compose_debug(row, row + 1, startcol, endcol, dbghl_normal, endcol >= clearcol);
601    compose_debug(row, row + 1, endcol, clearcol, dbghl_clear, true);
602#ifndef NDEBUG
603    for (int i = 0; i < endcol - startcol; i++) {
604      assert(attrs[i] >= 0);
605    }
606#endif
607    ui_composed_call_raw_line(1, row, startcol, endcol, clearcol, clearattr,
608                              flags, chunk, attrs);
609  }
610}
611
612/// The screen is invalid and will soon be cleared
613///
614/// Don't redraw floats until screen is cleared
615bool ui_comp_set_screen_valid(bool valid)
616{
617  bool old_val = valid_screen;
618  valid_screen = valid;
619  if (!valid) {
620    msg_sep_row = -1;
621  }
622  return old_val;
623}
624
625void ui_comp_msg_set_pos(Integer grid, Integer row, Boolean scrolled, String sep_char,
626                         Integer zindex, Integer compindex)
627{
628  msg_grid.pending_comp_index_update = true;
629  msg_grid.comp_row = (int)row;
630  if (scrolled && row > 0) {
631    msg_sep_row = (int)row - 1;
632    if (sep_char.data) {
633      msg_sep_char = schar_from_buf(sep_char.data, sep_char.size);
634    }
635  } else {
636    msg_sep_row = -1;
637  }
638
639  if (row > msg_current_row && ui_comp_should_draw()) {
640    compose_area(MAX(msg_current_row - 1, 0), row, 0, default_grid.cols);
641  } else if (row < msg_current_row && ui_comp_should_draw()
642             && (msg_current_row < Rows || (scrolled && !msg_was_scrolled))) {
643    int delta = msg_current_row - (int)row;
644    if (msg_grid.blending) {
645      int first_row = MAX((int)row - (scrolled ? 1 : 0), 0);
646      compose_area(first_row, Rows - delta, 0, Columns);
647    } else {
648      // scroll separator together with message text
649      int first_row = MAX((int)row - (msg_was_scrolled ? 1 : 0), 0);
650      ui_composed_call_grid_scroll(1, first_row, Rows, 0, Columns, delta, 0);
651      if (scrolled && !msg_was_scrolled && row > 0) {
652        compose_area(row - 1, row, 0, Columns);
653      }
654    }
655  }
656
657  msg_current_row = (int)row;
658  msg_was_scrolled = scrolled;
659}
660
661/// check if curgrid is covered by any other grid within the rectangle
662static bool curgrid_covered_above(int top, int bot, int left, int right)
663{
664  // check all layers above curgrid. if any intersect with the given rectangle, then consider the
665  // curgrid covered. account for the msg_sep_row if the msg_grid layer was scrolled.
666  for (size_t i = curgrid->comp_index + 1; i < kv_size(layers); i++) {
667    ScreenGrid *g = kv_A(layers, i);
668    int grid_top = g->comp_row - (g == &msg_grid && msg_was_scrolled ? 1 : 0);
669    int grid_bot = g->comp_row + g->comp_height - 1;
670    int grid_left = g->comp_col;
671    int grid_right = g->comp_col + g->comp_width - 1;
672
673    if (right >= grid_left && left <= grid_right && bot >= grid_top && top <= grid_bot) {
674      return true;
675    }
676  }
677
678  return false;
679}
680
681void ui_comp_grid_scroll(Integer grid, Integer top, Integer bot, Integer left, Integer right,
682                         Integer rows, Integer cols)
683{
684  if (!ui_comp_should_draw() || !ui_comp_set_grid((int)grid)) {
685    return;
686  }
687  top += curgrid->comp_row;
688  bot += curgrid->comp_row;
689  left += curgrid->comp_col;
690  right += curgrid->comp_col;
691  bool covered = curgrid_covered_above((int)top, (int)bot, (int)left, (int)right);
692
693  if (covered || curgrid->blending) {
694    // TODO(bfredl):
695    // 1. check if rectangles actually overlap
696    // 2. calculate subareas that can scroll.
697    compose_debug(top, bot, left, right, dbghl_recompose, true);
698    for (int r = (int)(top + MAX(-rows, 0)); r < bot - MAX(rows, 0); r++) {
699      // TODO(bfredl): workaround for win_update() performing two scrolls in a
700      // row, where the latter might scroll invalid space created by the first.
701      // ideally win_update() should keep track of this itself and not scroll
702      // the invalid space.
703      if (curgrid->attrs[curgrid->line_offset[r - curgrid->comp_row]
704                         + (size_t)left - (size_t)curgrid->comp_col] >= 0) {
705        compose_line(r, left, right, 0);
706      }
707    }
708  } else {
709    ui_composed_call_grid_scroll(1, top, bot, left, right, rows, cols);
710    if (rdb_flags & kOptRdbFlagCompositor) {
711      debug_delay(2);
712    }
713  }
714}
715
716void ui_comp_grid_resize(Integer grid, Integer width, Integer height)
717{
718  if (grid == 1) {
719    ui_composed_call_grid_resize(1, width, height);
720#ifndef NDEBUG
721    chk_width = (int)width;
722    chk_height = (int)height;
723#endif
724    size_t new_bufsize = (size_t)width;
725    if (bufsize != new_bufsize) {
726      xfree(linebuf);
727      xfree(attrbuf);
728      linebuf = xmalloc(new_bufsize * sizeof(*linebuf));
729      attrbuf = xmalloc(new_bufsize * sizeof(*attrbuf));
730      bufsize = new_bufsize;
731    }
732  }
733}