robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
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{ 59kv_push (layers ,& default_grid ); 60curgrid = & default_grid ; 61} 62 63#ifdef EXITFREE 64void ui_comp_free_all_mem (void ) 65{ 66kv_destroy (layers ); 67xfree (linebuf ); 68xfree (attrbuf ); 69} 70#endif 71 72void ui_comp_syn_init (void ) 73{ 74dbghl_normal = syn_check_group (S_LEN ("RedrawDebugNormal" )); 75dbghl_clear = syn_check_group (S_LEN ("RedrawDebugClear" )); 76dbghl_composed = syn_check_group (S_LEN ("RedrawDebugComposed" )); 77dbghl_recompose = syn_check_group (S_LEN ("RedrawDebugRecompose" )); 78} 79 80void ui_comp_attach (RemoteUI * ui ) 81{ 82composed_uis ++ ; 83ui -> composed = true; 84} 85 86void ui_comp_detach (RemoteUI * ui ) 87{ 88composed_uis -- ; 89if (composed_uis == 0 ) { 90XFREE_CLEAR (linebuf ); 91XFREE_CLEAR (attrbuf ); 92bufsize = 0 ; 93 } 94ui -> composed = false; 95} 96 97bool ui_comp_should_draw (void ) 98{ 99return 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{ 111size_t size = layers .size ; 112ScreenGrid * layer = layers .items [layer_idx ]; 113 114if (raise ) { 115while (layer_idx < size - 1 && layer -> zindex > layers .items [layer_idx + 1 ]-> zindex ) { 116layers .items [layer_idx ]= layers .items [layer_idx + 1 ]; 117layers .items [layer_idx ]-> comp_index = layer_idx ; 118layers .items [layer_idx ]-> pending_comp_index_update = true; 119layer_idx ++ ; 120 } 121 }else { 122while (layer_idx > 0 && layer -> zindex < layers .items [layer_idx - 1 ]-> zindex ) { 123layers .items [layer_idx ]= layers .items [layer_idx - 1 ]; 124layers .items [layer_idx ]-> comp_index = layer_idx ; 125layers .items [layer_idx ]-> pending_comp_index_update = true; 126layer_idx -- ; 127 } 128 } 129layers .items [layer_idx ]= layer ; 130layer -> comp_index = layer_idx ; 131layer -> 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 , 141bool on_top ) 142{ 143bool moved ; 144grid -> pending_comp_index_update = true; 145 146if (grid -> comp_index != 0 ) { 147moved = (row != grid -> comp_row )|| (col != grid -> comp_col ); 148if (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. 152grid -> comp_disabled = true; 153compose_area (grid -> comp_row ,row , 154grid -> comp_col ,grid -> comp_col + grid -> comp_width ); 155if (grid -> comp_col < col ) { 156compose_area (MAX (row ,grid -> comp_row ), 157MIN (row + height ,grid -> comp_row + grid -> comp_height ), 158grid -> comp_col ,col ); 159 } 160if (col + width < grid -> comp_col + grid -> comp_width ) { 161compose_area (MAX (row ,grid -> comp_row ), 162MIN (row + height ,grid -> comp_row + grid -> comp_height ), 163col + width ,grid -> comp_col + grid -> comp_width ); 164 } 165compose_area (row + height ,grid -> comp_row + grid -> comp_height , 166grid -> comp_col ,grid -> comp_col + grid -> comp_width ); 167grid -> comp_disabled = false; 168 } 169grid -> comp_row = row ; 170grid -> comp_col = col ; 171 }else { 172moved = true; 173#ifndef NDEBUG 174for (size_t i = 0 ;i < kv_size (layers );i ++ ) { 175if (kv_A (layers ,i )== grid ) { 176abort (); 177 } 178 } 179#endif 180 181size_t insert_at = kv_size (layers ); 182while (insert_at > 0 && kv_A (layers ,insert_at - 1 )-> zindex > grid -> zindex ) { 183insert_at -- ; 184 } 185 186if (curwin && kv_A (layers ,insert_at - 1 )== & curwin -> w_grid_alloc 187&& kv_A (layers ,insert_at - 1 )-> zindex == grid -> zindex 188&& !on_top ) { 189insert_at -- ; 190 } 191// not found: new grid 192kv_pushp (layers ); 193for (size_t i = kv_size (layers )- 1 ;i > insert_at ;i -- ) { 194kv_A (layers ,i )= kv_A (layers ,i - 1 ); 195kv_A (layers ,i )-> comp_index = i ; 196kv_A (layers ,i )-> pending_comp_index_update = true; 197 } 198kv_A (layers ,insert_at )= grid ; 199 200grid -> comp_row = row ; 201grid -> comp_col = col ; 202grid -> comp_index = insert_at ; 203grid -> pending_comp_index_update = true; 204 } 205 206grid -> comp_height = height ; 207grid -> comp_width = width ; 208if (moved && valid && ui_comp_should_draw ()) { 209compose_area (grid -> comp_row ,grid -> comp_row + grid -> rows , 210grid -> comp_col ,grid -> comp_col + grid -> cols ); 211 } 212return moved ; 213} 214 215void ui_comp_remove_grid (ScreenGrid * grid ) 216{ 217assert (grid != & default_grid ); 218if (grid -> comp_index == 0 ) { 219// grid wasn't present 220return ; 221 } 222 223if (curgrid == grid ) { 224curgrid = & default_grid ; 225 } 226 227for (size_t i = grid -> comp_index ;i < kv_size (layers )- 1 ;i ++ ) { 228kv_A (layers ,i )= kv_A (layers ,i + 1 ); 229kv_A (layers ,i )-> comp_index = i ; 230kv_A (layers ,i )-> pending_comp_index_update = true; 231 } 232 (void )kv_pop (layers ); 233grid -> comp_index = 0 ; 234grid -> 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 238ui_comp_compose_grid (grid ); 239} 240 241bool ui_comp_set_grid (handle_T handle ) 242{ 243if (curgrid -> handle == handle ) { 244return true; 245 } 246ScreenGrid * grid = NULL ; 247for (size_t i = 0 ;i < kv_size (layers );i ++ ) { 248if (kv_A (layers ,i )-> handle == handle ) { 249grid = kv_A (layers ,i ); 250break ; 251 } 252 } 253if (grid != NULL ) { 254curgrid = grid ; 255return true; 256 } 257return false; 258} 259 260void ui_comp_raise_grid (ScreenGrid * grid ,size_t new_index ) 261{ 262size_t old_index = grid -> comp_index ; 263for (size_t i = old_index ;i < new_index ;i ++ ) { 264kv_A (layers ,i )= kv_A (layers ,i + 1 ); 265kv_A (layers ,i )-> comp_index = i ; 266kv_A (layers ,i )-> pending_comp_index_update = true; 267 } 268kv_A (layers ,new_index )= grid ; 269grid -> comp_index = new_index ; 270grid -> pending_comp_index_update = true; 271for (size_t i = old_index ;i < new_index ;i ++ ) { 272ScreenGrid * grid2 = kv_A (layers ,i ); 273int startcol = MAX (grid -> comp_col ,grid2 -> comp_col ); 274int endcol = MIN (grid -> comp_col + grid -> cols , 275grid2 -> comp_col + grid2 -> cols ); 276compose_area (MAX (grid -> comp_row ,grid2 -> comp_row ), 277MIN (grid -> comp_row + grid -> rows ,grid2 -> comp_row + grid2 -> rows ), 278startcol ,endcol ); 279 } 280} 281 282void ui_comp_grid_cursor_goto (Integer grid_handle ,Integer r ,Integer c ) 283{ 284if (!ui_comp_set_grid ((int )grid_handle )) { 285return ; 286 } 287int cursor_row = curgrid -> comp_row + (int )r ; 288int 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() 292if (curgrid != & default_grid ) { 293size_t new_index = kv_size (layers )- 1 ; 294 295while (new_index > 1 && kv_A (layers ,new_index )-> zindex > curgrid -> zindex ) { 296new_index -- ; 297 } 298 299if (curgrid -> comp_index < new_index ) { 300ui_comp_raise_grid (curgrid ,new_index ); 301 } 302 } 303 304if (cursor_col >=default_grid .cols || cursor_row >=default_grid .rows ) { 305// TODO(bfredl): this happens with 'writedelay', refactor? 306// abort(); 307return ; 308 } 309ui_composed_call_grid_cursor_goto (1 ,cursor_row ,cursor_col ); 310} 311 312ScreenGrid * ui_comp_mouse_focus (int row ,int col ) 313{ 314for (ssize_t i = (ssize_t )kv_size (layers )- 1 ;i > 0 ;i -- ) { 315ScreenGrid * grid = kv_A (layers ,i ); 316if (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 ) { 319return grid ; 320 } 321 } 322if (ui_has (kUIMultigrid )) { 323FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 324ScreenGrid * grid = & wp -> w_grid_alloc ; 325if (grid -> mouse_enabled && row >=wp -> w_winrow && row < wp -> w_winrow + grid -> rows 326&& col >=wp -> w_wincol && col < wp -> w_wincol + grid -> cols ) { 327return grid ; 328 } 329 } 330 } 331return 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{ 337for (ssize_t i = (ssize_t )kv_size (layers )- 1 ;i > 0 ;i -- ) { 338ScreenGrid * grid = kv_A (layers ,i ); 339if (row >=grid -> comp_row && row < grid -> comp_row + grid -> rows 340&& col >=grid -> comp_col && col < grid -> comp_col + grid -> cols ) { 341return grid ; 342 } 343 } 344 345FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 346ScreenGrid * grid = & wp -> w_grid_alloc ; 347if (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 ) { 350return grid ; 351 } 352 } 353return & 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. 363startcol = 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. 366int skipstart = 0 ; 367int skipend = 0 ; 368if (startcol > 0 && (flags & kLineFlagInvalid )) { 369startcol -- ; 370skipstart = 1 ; 371 } 372if (endcol < default_grid .cols && (flags & kLineFlagInvalid )) { 373endcol ++ ; 374skipend = 1 ; 375 } 376 377int col = (int )startcol ; 378ScreenGrid * grid = NULL ; 379schar_T * bg_line = & default_grid .chars [default_grid .line_offset [row ] 380+ (size_t )startcol ]; 381sattr_T * bg_attrs = & default_grid .attrs [default_grid .line_offset [row ] 382+ (size_t )startcol ]; 383 384while (col < endcol ) { 385int until = 0 ; 386for (size_t i = 0 ;i < kv_size (layers );i ++ ) { 387ScreenGrid * 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. 393int grid_width = MIN (g -> cols ,g -> comp_width ); 394int grid_height = MIN (g -> rows ,g -> comp_height ); 395if (g -> comp_row > row || row >=g -> comp_row + grid_height 396|| g -> comp_disabled ) { 397continue ; 398 } 399if (g -> comp_col <=col && col < g -> comp_col + grid_width ) { 400grid = g ; 401until = g -> comp_col + grid_width ; 402 }else if (g -> comp_col > col ) { 403until = MIN (until ,g -> comp_col ); 404 } 405 } 406until = MIN (until , (int )endcol ); 407 408assert (grid != NULL ); 409assert (until > col ); 410assert (until <=default_grid .cols ); 411size_t n = (size_t )(until - col ); 412 413if (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. 416grid = & msg_grid ; 417sattr_T msg_sep_attr = (sattr_T )HL_ATTR (HLF_MSGSEP ); 418for (int i = col ;i < until ;i ++ ) { 419linebuf [i - startcol ]= msg_sep_char ; 420attrbuf [i - startcol ]= msg_sep_attr ; 421 } 422 }else { 423size_t off = grid -> line_offset [row - grid -> comp_row ] 424+ (size_t )(col - grid -> comp_col ); 425memcpy (linebuf + (col - startcol ),grid -> chars + off ,n * sizeof (* linebuf )); 426memcpy (attrbuf + (col - startcol ),grid -> attrs + off ,n * sizeof (* attrbuf )); 427if (grid -> comp_col + grid -> cols > until 428&& grid -> chars [off + n ]== NUL ) { 429linebuf [until - 1 - startcol ]= schar_from_ascii (' ' ); 430if (col == startcol && n == 1 ) { 431skipstart = 0 ; 432 } 433 } 434 } 435 436// 'pumblend' and 'winblend' 437if (grid -> blending ) { 438int width ; 439for (int i = col - (int )startcol ;i < until - startcol ;i += width ) { 440width = 1 ; 441// negative space 442bool thru = (linebuf [i ]== schar_from_ascii (' ' ) 443|| linebuf [i ]== schar_from_char (L'\u2800' ))&& bg_line [i ]!= NUL ; 444if (i + 1 < endcol - startcol && bg_line [i + 1 ]== NUL ) { 445width = 2 ; 446thru &= (linebuf [i + 1 ]== schar_from_ascii (' ' ) 447|| linebuf [i + 1 ]== schar_from_char (L'\u2800' )); 448 } 449attrbuf [i ]= (sattr_T )hl_blend_attrs (bg_attrs [i ],attrbuf [i ],& thru ); 450if (width == 2 ) { 451attrbuf [i + 1 ]= (sattr_T )hl_blend_attrs (bg_attrs [i + 1 ], 452attrbuf [i + 1 ],& thru ); 453 } 454if (thru ) { 455memcpy (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. 462if (linebuf [col - startcol ]== NUL ) { 463linebuf [col - startcol ]= schar_from_ascii (' ' ); 464if (col == endcol - 1 ) { 465skipend = 0 ; 466 } 467 }else if (col == startcol && n > 1 && linebuf [1 ]== NUL ) { 468skipstart = 0 ; 469 } 470 471col = until ; 472 } 473if (linebuf [endcol - startcol - 1 ]== NUL ) { 474skipend = 0 ; 475 } 476 477assert (endcol <=chk_width ); 478assert (row < chk_height ); 479 480if (!(grid && (grid == & default_grid || (grid -> comp_col == 0 && grid -> cols == Columns )))) { 481flags = flags & ~kLineFlagWrap ; 482 } 483 484for (int i = skipstart ;i < (endcol - skipend )- startcol ;i ++ ) { 485if (attrbuf [i ]< 0 ) { 486if (rdb_flags & kOptRdbFlagInvalid ) { 487abort (); 488 }else { 489attrbuf [i ]= 0 ; 490 } 491 } 492 } 493ui_composed_call_raw_line (1 ,row ,startcol + skipstart , 494endcol - 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 , 500int syn_id ,bool delay ) 501{ 502if (!(rdb_flags & kOptRdbFlagCompositor )|| startcol >=endcol ) { 503return ; 504 } 505 506endrow = MIN (endrow ,default_grid .rows ); 507endcol = MIN (endcol ,default_grid .cols ); 508int attr = syn_id2attr (syn_id ); 509 510if (delay ) { 511debug_delay (endrow - startrow ); 512 } 513 514for (int row = (int )startrow ;row < endrow ;row ++ ) { 515ui_composed_call_raw_line (1 ,row ,startcol ,startcol ,endcol ,attr , false, 516 (const schar_T * )linebuf , 517 (const sattr_T * )attrbuf ); 518 } 519 520if (delay ) { 521debug_delay (endrow - startrow ); 522 } 523} 524 525static void debug_delay (Integer lines ) 526{ 527ui_call_flush (); 528uint64_t wd = (uint64_t )llabs (p_wd ); 529uint64_t factor = (uint64_t )MAX (MIN (lines ,5 ),1 ); 530os_sleep (factor * wd ); 531} 532 533static void compose_area (Integer startrow ,Integer endrow ,Integer startcol ,Integer endcol ) 534{ 535compose_debug (startrow ,endrow ,startcol ,endcol ,dbghl_recompose , true); 536endrow = MIN (endrow ,default_grid .rows ); 537endcol = MIN (endcol ,default_grid .cols ); 538if (endcol <=startcol ) { 539return ; 540 } 541for (int r = (int )startrow ;r < endrow ;r ++ ) { 542compose_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{ 552if (ui_comp_should_draw ()) { 553compose_area (grid -> comp_row ,grid -> comp_row + grid -> rows , 554grid -> 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 , 559Integer clearattr ,LineFlags flags ,const schar_T * chunk , 560const sattr_T * attrs ) 561{ 562if (!ui_comp_should_draw ()|| !ui_comp_set_grid ((int )grid )) { 563return ; 564 } 565 566row += curgrid -> comp_row ; 567startcol += curgrid -> comp_col ; 568endcol += curgrid -> comp_col ; 569clearcol += curgrid -> comp_col ; 570if (curgrid != & default_grid ) { 571flags = flags & ~kLineFlagWrap ; 572 } 573 574assert (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. 579if (row >=default_grid .rows ) { 580DLOG ("compositor: invalid row %" PRId64 " on grid %" PRId64 ,row ,grid ); 581return ; 582 } 583if (clearcol > default_grid .cols ) { 584DLOG ("compositor: invalid last column %" PRId64 " on grid %" PRId64 , 585clearcol ,grid ); 586if (startcol >=default_grid .cols ) { 587return ; 588 } 589clearcol = default_grid .cols ; 590endcol = MIN (endcol ,clearcol ); 591 } 592 593bool 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. 596if (flags & kLineFlagInvalid || covered || curgrid -> blending ) { 597compose_debug (row ,row + 1 ,startcol ,clearcol ,dbghl_composed , true); 598compose_line (row ,startcol ,clearcol ,flags ); 599 }else { 600compose_debug (row ,row + 1 ,startcol ,endcol ,dbghl_normal ,endcol >=clearcol ); 601compose_debug (row ,row + 1 ,endcol ,clearcol ,dbghl_clear , true); 602#ifndef NDEBUG 603for (int i = 0 ;i < endcol - startcol ;i ++ ) { 604assert (attrs [i ] >=0 ); 605 } 606#endif 607ui_composed_call_raw_line (1 ,row ,startcol ,endcol ,clearcol ,clearattr , 608flags ,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{ 617bool old_val = valid_screen ; 618valid_screen = valid ; 619if (!valid ) { 620msg_sep_row = -1 ; 621 } 622return old_val ; 623} 624 625void ui_comp_msg_set_pos (Integer grid ,Integer row ,Boolean scrolled ,String sep_char , 626Integer zindex ,Integer compindex ) 627{ 628msg_grid .pending_comp_index_update = true; 629msg_grid .comp_row = (int )row ; 630if (scrolled && row > 0 ) { 631msg_sep_row = (int )row - 1 ; 632if (sep_char .data ) { 633msg_sep_char = schar_from_buf (sep_char .data ,sep_char .size ); 634 } 635 }else { 636msg_sep_row = -1 ; 637 } 638 639if (row > msg_current_row && ui_comp_should_draw ()) { 640compose_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 ))) { 643int delta = msg_current_row - (int )row ; 644if (msg_grid .blending ) { 645int first_row = MAX ((int )row - (scrolled ?1 :0 ),0 ); 646compose_area (first_row ,Rows - delta ,0 ,Columns ); 647 }else { 648// scroll separator together with message text 649int first_row = MAX ((int )row - (msg_was_scrolled ?1 :0 ),0 ); 650ui_composed_call_grid_scroll (1 ,first_row ,Rows ,0 ,Columns ,delta ,0 ); 651if (scrolled && !msg_was_scrolled && row > 0 ) { 652compose_area (row - 1 ,row ,0 ,Columns ); 653 } 654 } 655 } 656 657msg_current_row = (int )row ; 658msg_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. 666for (size_t i = curgrid -> comp_index + 1 ;i < kv_size (layers );i ++ ) { 667ScreenGrid * g = kv_A (layers ,i ); 668int grid_top = g -> comp_row - (g == & msg_grid && msg_was_scrolled ?1 :0 ); 669int grid_bot = g -> comp_row + g -> comp_height - 1 ; 670int grid_left = g -> comp_col ; 671int grid_right = g -> comp_col + g -> comp_width - 1 ; 672 673if (right >=grid_left && left <=grid_right && bot >=grid_top && top <=grid_bot ) { 674return true; 675 } 676 } 677 678return false; 679} 680 681void ui_comp_grid_scroll (Integer grid ,Integer top ,Integer bot ,Integer left ,Integer right , 682Integer rows ,Integer cols ) 683{ 684if (!ui_comp_should_draw ()|| !ui_comp_set_grid ((int )grid )) { 685return ; 686 } 687top += curgrid -> comp_row ; 688bot += curgrid -> comp_row ; 689left += curgrid -> comp_col ; 690right += curgrid -> comp_col ; 691bool covered = curgrid_covered_above ((int )top , (int )bot , (int )left , (int )right ); 692 693if (covered || curgrid -> blending ) { 694// TODO(bfredl): 695// 1. check if rectangles actually overlap 696// 2. calculate subareas that can scroll. 697compose_debug (top ,bot ,left ,right ,dbghl_recompose , true); 698for (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. 703if (curgrid -> attrs [curgrid -> line_offset [r - curgrid -> comp_row ] 704+ (size_t )left - (size_t )curgrid -> comp_col ] >=0 ) { 705compose_line (r ,left ,right ,0 ); 706 } 707 } 708 }else { 709ui_composed_call_grid_scroll (1 ,top ,bot ,left ,right ,rows ,cols ); 710if (rdb_flags & kOptRdbFlagCompositor ) { 711debug_delay (2 ); 712 } 713 } 714} 715 716void ui_comp_grid_resize (Integer grid ,Integer width ,Integer height ) 717{ 718if (grid == 1 ) { 719ui_composed_call_grid_resize (1 ,width ,height ); 720#ifndef NDEBUG 721chk_width = (int )width ; 722chk_height = (int )height ; 723#endif 724size_t new_bufsize = (size_t )width ; 725if (bufsize != new_bufsize ) { 726xfree (linebuf ); 727xfree (attrbuf ); 728linebuf = xmalloc (new_bufsize * sizeof (* linebuf )); 729attrbuf = xmalloc (new_bufsize * sizeof (* attrbuf )); 730bufsize = new_bufsize ; 731 } 732 } 733}