Codebergdnkl
foot
master
1#include "selection.h"
3#include <ctype.h>
4#include <string.h>
5#include <unistd.h>
6#include <fcntl.h>
7#include <errno.h>
9#include <sys/epoll.h>
10#include <sys/timerfd.h>
12#include <pixman.h>
14#define LOG_MODULE "selection"
15#define LOG_ENABLE_DBG 0
16#include "log.h"
18#include "async.h"
19#include "char32.h"
20#include "commands.h"
21#include "config.h"
22#include "debug.h"
23#include "extract.h"
24#include "grid.h"
25#include "misc.h"
26#include "render.h"
27#include "search.h"
28#include "uri.h"
29#include "util.h"
30#include "vt.h"
31#include "xmalloc.h"
33static const char *const mime_type_map[] = {
34 [DATA_OFFER_MIME_UNSET] = NULL,
35 [DATA_OFFER_MIME_TEXT_PLAIN] = "text/plain",
36 [DATA_OFFER_MIME_TEXT_UTF8] = "text/plain;charset=utf-8",
37 [DATA_OFFER_MIME_URI_LIST] = "text/uri-list",
39 [DATA_OFFER_MIME_TEXT_TEXT] = "TEXT",
40 [DATA_OFFER_MIME_TEXT_STRING] = "STRING",
41 [DATA_OFFER_MIME_TEXT_UTF8_STRING] = "UTF8_STRING",
42};
44static inline struct coord
45bounded(const struct grid *grid, struct coord coord)
46{
47 coord.row &= grid->num_rows - 1;
48 return coord;
49}
51struct coord
52selection_get_start(const struct terminal *term)
53{
54 if (term->selection.coords.start.row < 0)
55 return term->selection.coords.start;
56 return bounded(term->grid, term->selection.coords.start);
57}
59struct coord
60selection_get_end(const struct terminal *term)
61{
62 if (term->selection.coords.end.row < 0)
63 return term->selection.coords.end;
64 return bounded(term->grid, term->selection.coords.end);
65}
67bool
68selection_on_rows(const struct terminal *term, int row_start, int row_end)
69{
70 xassert(term->selection.coords.end.row >= 0);
72 LOG_DBG("on rows: %d-%d, range: %d-%d (offset=%d)",
73 term->selection.coords.start.row, term->selection.coords.end.row,
74 row_start, row_end, term->grid->offset);
76 row_start += term->grid->offset;
77 row_end += term->grid->offset;
78 xassert(row_end >= row_start);
80 const struct coord *start = &term->selection.coords.start;
81 const struct coord *end = &term->selection.coords.end;
83 const struct grid *grid = term->grid;
84 const int sb_start = grid->offset + term->rows;
86 /* Use scrollback relative coords when checking for overlap */
87 const int rel_row_start =
88 grid_row_abs_to_sb_precalc_sb_start(grid, sb_start, row_start);
89 const int rel_row_end =
90 grid_row_abs_to_sb_precalc_sb_start(grid, sb_start, row_end);
91 int rel_sel_start =
92 grid_row_abs_to_sb_precalc_sb_start(grid, sb_start, start->row);
93 int rel_sel_end =
94 grid_row_abs_to_sb_precalc_sb_start(grid, sb_start, end->row);
96 if (rel_sel_start > rel_sel_end) {
97 int tmp = rel_sel_start;
98 rel_sel_start = rel_sel_end;
99 rel_sel_end = tmp;
100 }
102 if ((rel_row_start <= rel_sel_start && rel_row_end >= rel_sel_start) ||
103 (rel_row_start <= rel_sel_end && rel_row_end >= rel_sel_end))
104 {
105 /* The range crosses one of the selection boundaries */
106 return true;
107 }
109 if (rel_row_start >= rel_sel_start && rel_row_end <= rel_sel_end)
110 return true;
112 return false;
113}
115void
116selection_scroll_up(struct terminal *term, int rows)
117{
118 xassert(term->selection.coords.end.row >= 0);
120 const int rel_row_start =
121 grid_row_abs_to_sb(term->grid, term->rows, term->selection.coords.start.row);
122 const int rel_row_end =
123 grid_row_abs_to_sb(term->grid, term->rows, term->selection.coords.end.row);
124 const int actual_start = min(rel_row_start, rel_row_end);
126 if (actual_start - rows < 0) {
127 /* Part of the selection will be scrolled out, cancel it */
128 selection_cancel(term);
129 }
130}
132void
133selection_scroll_down(struct terminal *term, int rows)
134{
135 xassert(term->selection.coords.end.row >= 0);
137 const struct grid *grid = term->grid;
138 const struct range *sel = &term->selection.coords;
140 const int screen_end =
141 grid_row_abs_to_sb(grid, term->rows, grid->offset + term->rows - 1);
142 const int rel_row_start =
143 grid_row_abs_to_sb(term->grid, term->rows, sel->start.row);
144 const int rel_row_end =
145 grid_row_abs_to_sb(term->grid, term->rows, sel->end.row);
146 const int actual_end = max(rel_row_start, rel_row_end);
148 if (actual_end > screen_end - rows) {
149 /* Part of the selection will be scrolled out, cancel it */
150 selection_cancel(term);
151 }
152}
154void
155selection_view_up(struct terminal *term, int new_view)
156{
157 if (likely(term->selection.coords.start.row < 0))
158 return;
160 if (likely(new_view < term->grid->view))
161 return;
163 term->selection.coords.start.row += term->grid->num_rows;
164 if (term->selection.coords.end.row >= 0)
165 term->selection.coords.end.row += term->grid->num_rows;
166}
168void
169selection_view_down(struct terminal *term, int new_view)
170{
171 if (likely(term->selection.coords.start.row < 0))
172 return;
174 if (likely(new_view > term->grid->view))
175 return;
177 term->selection.coords.start.row &= term->grid->num_rows - 1;
178 if (term->selection.coords.end.row >= 0)
179 term->selection.coords.end.row &= term->grid->num_rows - 1;
180}
182static void
183foreach_selected_normal(
184 struct terminal *term, struct coord _start, struct coord _end,
185 bool (*cb)(struct terminal *term, struct row *row, struct cell *cell,
186 int row_no, int col, void *data),
187 void *data)
188{
189 const struct coord *start = &_start;
190 const struct coord *end = &_end;
192 const int grid_rows = term->grid->num_rows;
194 /* Start/end rows, relative to the scrollback start */
195 /* Start/end rows, relative to the scrollback start */
196 const int rel_start_row =
197 grid_row_abs_to_sb(term->grid, term->rows, start->row);
198 const int rel_end_row =
199 grid_row_abs_to_sb(term->grid, term->rows, end->row);
201 int start_row, end_row;
202 int start_col, end_col;
204 if (rel_start_row < rel_end_row) {
205 start_row = start->row;
206 start_col = start->col;
207 end_row = end->row;
208 end_col = end->col;
209 } else if (rel_start_row > rel_end_row) {
210 start_row = end->row;
211 start_col = end->col;
212 end_row = start->row;
213 end_col = start->col;
214 } else {
215 start_row = end_row = start->row;
216 start_col = min(start->col, end->col);
217 end_col = max(start->col, end->col);
218 }
220 start_row &= (grid_rows - 1);
221 end_row &= (grid_rows - 1);
223 for (int r = start_row; r != end_row; r = (r + 1) & (grid_rows - 1)) {
224 struct row *row = term->grid->rows[r];
225 xassert(row != NULL);
227 for (int c = start_col; c <= term->cols - 1; c++) {
228 if (!cb(term, row, &row->cells[c], r, c, data))
229 return;
230 }
232 start_col = 0;
233 }
235 /* Last, partial row */
236 struct row *row = term->grid->rows[end_row];
237 xassert(row != NULL);
239 for (int c = start_col; c <= end_col; c++) {
240 if (!cb(term, row, &row->cells[c], end_row, c, data))
241 return;
242 }
243}
245static void
246foreach_selected_block(
247 struct terminal *term, struct coord _start, struct coord _end,
248 bool (*cb)(struct terminal *term, struct row *row, struct cell *cell,
249 int row_no, int col, void *data),
250 void *data)
251{
252 const struct coord *start = &_start;
253 const struct coord *end = &_end;
255 const int grid_rows = term->grid->num_rows;
257 /* Start/end rows, relative to the scrollback start */
258 const int rel_start_row =
259 grid_row_abs_to_sb(term->grid, term->rows, start->row);
260 const int rel_end_row =
261 grid_row_abs_to_sb(term->grid, term->rows, end->row);
263 struct coord top_left = {
264 .row = (rel_start_row < rel_end_row
265 ? start->row : end->row) & (grid_rows - 1),
266 .col = min(start->col, end->col),
267 };
269 struct coord bottom_right = {
270 .row = (rel_start_row > rel_end_row
271 ? start->row : end->row) & (grid_rows - 1),
272 .col = max(start->col, end->col),
273 };
275 int r = top_left.row;
276 while (true) {
277 struct row *row = term->grid->rows[r];
278 xassert(row != NULL);
280 for (int c = top_left.col; c <= bottom_right.col; c++) {
281 if (!cb(term, row, &row->cells[c], r, c, data))
282 return;
283 }
285 if (r == bottom_right.row)
286 break;
288 r++;
289 r &= grid_rows - 1;
290 }
291}
293static void
294foreach_selected(
295 struct terminal *term, struct coord start, struct coord end,
296 bool (*cb)(struct terminal *term, struct row *row, struct cell *cell, int row_no, int col, void *data),
297 void *data)
298{
299 switch (term->selection.kind) {
300 case SELECTION_CHAR_WISE:
301 case SELECTION_WORD_WISE:
302 case SELECTION_QUOTE_WISE:
303 case SELECTION_LINE_WISE:
304 foreach_selected_normal(term, start, end, cb, data);
305 return;
307 case SELECTION_BLOCK:
308 foreach_selected_block(term, start, end, cb, data);
309 return;
311 case SELECTION_NONE:
312 break;
313 }
315 BUG("Invalid selection kind");
316}
318static bool
319extract_one_const_wrapper(struct terminal *term,
320 struct row *row, struct cell *cell,
321 int row_no, int col, void *data)
322{
323 return extract_one(term, row, cell, col, data);
324}
326char *
327selection_to_text(const struct terminal *term)
328{
329 if (term->selection.coords.end.row == -1)
330 return NULL;
332 struct extraction_context *ctx = extract_begin(term->selection.kind, true);
333 if (ctx == NULL)
334 return NULL;
336 foreach_selected(
337 (struct terminal *)term, term->selection.coords.start, term->selection.coords.end,
338 &extract_one_const_wrapper, ctx);
340 char *text;
341 return extract_finish(ctx, &text, NULL) ? text : NULL;
342}
344/* Coordinates are in *absolute* row numbers (NOT view local) */
345void
346selection_find_word_boundary_left(const struct terminal *term, struct coord *pos,
347 bool spaces_only)
348{
349 const struct grid *grid = term->grid;
351 xassert(pos->col >= 0);
352 xassert(pos->col < term->cols);
353 xassert(pos->row >= 0);
354 pos->row &= grid->num_rows - 1;
356 const struct row *r = grid->rows[pos->row];
357 char32_t c = r->cells[pos->col].wc;
359 while (c >= CELL_SPACER) {
360 xassert(pos->col > 0);
361 if (pos->col == 0)
362 return;
363 pos->col--;
364 c = r->cells[pos->col].wc;
365 }
367 if (c >= CELL_COMB_CHARS_LO && c <= CELL_COMB_CHARS_HI)
368 c = composed_lookup(term->composed, c - CELL_COMB_CHARS_LO)->chars[0];
370 bool initial_is_space = c == 0 || isc32space(c);
371 bool initial_is_delim =
372 !initial_is_space && !isword(c, spaces_only, term->conf->word_delimiters);
373 bool initial_is_word =
374 c != 0 && isword(c, spaces_only, term->conf->word_delimiters);
376 while (true) {
377 int next_col = pos->col - 1;
378 int next_row = pos->row;
380 const struct row *row = grid->rows[next_row];
382 /* Linewrap */
383 if (next_col < 0) {
384 next_col = term->cols - 1;
386 next_row = (next_row - 1 + grid->num_rows) & (grid->num_rows - 1);
388 if (grid_row_abs_to_sb(grid, term->rows, next_row) == term->grid->num_rows - 1 ||
389 grid->rows[next_row] == NULL)
390 {
391 /* Scrollback wrap-around */
392 break;
393 }
395 row = grid->rows[next_row];
397 if (row->linebreak) {
398 /* Hard linebreak, treat as space. I.e. break selection */
399 break;
400 }
401 }
403 c = row->cells[next_col].wc;
404 while (c >= CELL_SPACER) {
405 xassert(next_col > 0);
406 if (--next_col < 0)
407 return;
408 c = row->cells[next_col].wc;
409 }
411 if (c >= CELL_COMB_CHARS_LO && c <= CELL_COMB_CHARS_HI)
412 c = composed_lookup(term->composed, c - CELL_COMB_CHARS_LO)->chars[0];
414 bool is_space = c == 0 || isc32space(c);
415 bool is_delim =
416 !is_space && !isword(c, spaces_only, term->conf->word_delimiters);
417 bool is_word =
418 c != 0 && isword(c, spaces_only, term->conf->word_delimiters);
420 if (initial_is_space && !is_space)
421 break;
422 if (initial_is_delim && !is_delim)
423 break;
424 if (initial_is_word && !is_word)
425 break;
427 pos->col = next_col;
428 pos->row = next_row;
429 }
430}
432/* Coordinates are in *absolute* row numbers (NOT view local) */
433void
434selection_find_word_boundary_right(const struct terminal *term, struct coord *pos,
435 bool spaces_only,
436 bool stop_on_space_to_word_boundary)
437{
438 const struct grid *grid = term->grid;
440 xassert(pos->col >= 0);
441 xassert(pos->col < term->cols);
442 xassert(pos->row >= 0);
443 pos->row &= grid->num_rows - 1;
445 const struct row *r = grid->rows[pos->row];
446 char32_t c = r->cells[pos->col].wc;
448 while (c >= CELL_SPACER) {
449 xassert(pos->col > 0);
450 if (pos->col == 0)
451 return;
452 pos->col--;
453 c = r->cells[pos->col].wc;
454 }
456 if (c >= CELL_COMB_CHARS_LO && c <= CELL_COMB_CHARS_HI)
457 c = composed_lookup(term->composed, c - CELL_COMB_CHARS_LO)->chars[0];
459 bool initial_is_space = c == 0 || isc32space(c);
460 bool initial_is_delim =
461 !initial_is_space && !isword(c, spaces_only, term->conf->word_delimiters);
462 bool initial_is_word =
463 c != 0 && isword(c, spaces_only, term->conf->word_delimiters);
464 bool have_seen_word = initial_is_word;
466 while (true) {
467 int next_col = pos->col;
468 int next_row = pos->row;
470 const struct row *row = term->grid->rows[next_row];
472 do {
473 /* Linewrap */
474 if (++next_col >= term->cols) {
475 if (row->linebreak) {
476 /* Hard linebreak, treat as space. I.e. break selection */
477 return;
478 }
480 next_col = 0;
481 next_row = (next_row + 1) & (grid->num_rows - 1);
483 if (grid_row_abs_to_sb(grid, term->rows, next_row) == 0) {
484 /* Scrollback wrap-around */
485 return;
486 }
488 row = grid->rows[next_row];
489 }
491 c = row->cells[next_col].wc;
492 } while (c >= CELL_SPACER);
494 if (c >= CELL_COMB_CHARS_LO && c <= CELL_COMB_CHARS_HI)
495 c = composed_lookup(term->composed, c - CELL_COMB_CHARS_LO)->chars[0];
497 bool is_space = c == 0 || isc32space(c);
498 bool is_delim =
499 !is_space && !isword(c, spaces_only, term->conf->word_delimiters);
500 bool is_word =
501 c != 0 && isword(c, spaces_only, term->conf->word_delimiters);
503 if (stop_on_space_to_word_boundary) {
504 if (initial_is_space && !is_space)
505 break;
506 if (initial_is_delim && !is_delim)
507 break;
508 } else {
509 if (initial_is_space && ((have_seen_word && is_space) || is_delim))
510 break;
511 if (initial_is_delim && ((have_seen_word && is_delim) || is_space))
512 break;
513 }
514 if (initial_is_word && !is_word)
515 break;
517 have_seen_word = is_word;
519 pos->col = next_col;
520 pos->row = next_row;
521 }
522}
524static bool
525selection_find_quote_left(struct terminal *term, struct coord *pos,
526 char32_t *quote_char)
527{
528 const struct row *row = grid_row_in_view(term->grid, pos->row);
529 char32_t wc = row->cells[pos->col].wc;
531 if (*quote_char == '\0' ? (wc == '"' || wc == '\'')
532 : wc == *quote_char)
533 {
534 return false;
535 }
537 int next_row = pos->row;
538 int next_col = pos->col;
540 while (true) {
541 if (--next_col < 0) {
542 next_col = term->cols - 1;
543 if (--next_row < 0)
544 return false;
546 row = grid_row_in_view(term->grid, next_row);
547 if (row->linebreak)
548 return false;
549 }
551 wc = row->cells[next_col].wc;
553 if (*quote_char == '\0' ? (wc == '"' || wc == '\'')
554 : wc == *quote_char)
555 {
556 xassert(next_col + 1 <= term->cols);
557 if (next_col + 1 == term->cols) {
558 xassert(next_row < pos->row);
559 pos->row = next_row + 1;
560 pos->col = 0;
561 } else {
562 pos->row = next_row;
563 pos->col = next_col + 1;
564 }
566 *quote_char = wc;
567 return true;
568 }
569 }
570}
572static bool
573selection_find_quote_right(struct terminal *term, struct coord *pos, char32_t quote_char)
574{
575 if (quote_char == '\0')
576 return false;
578 const struct row *row = grid_row_in_view(term->grid, pos->row);
579 char32_t wc = row->cells[pos->col].wc;
580 if (wc == quote_char)
581 return false;
583 int next_row = pos->row;
584 int next_col = pos->col;
586 while (true) {
587 if (++next_col >= term->cols) {
588 next_col = 0;
589 if (++next_row >= term->rows)
590 return false;
592 if (row->linebreak)
593 return false;
595 row = grid_row_in_view(term->grid, next_row);
596 }
598 wc = row->cells[next_col].wc;
599 if (wc == quote_char) {
600 if (next_col == 0) {
601 /* Selection coordinates are view-relative, and it
602 isn't possible to have a right-side quote with
603 next-col == 0 AND next_row == 0 */
604 xassert(next_row > 0);
605 pos->row = next_row - 1;
606 pos->col = term->cols - 1;
607 } else {
608 pos->row = next_row;
609 pos->col = next_col - 1;
610 }
611 return true;
612 }
613 }
614}
616static void
617selection_find_line_boundary_left(struct terminal *term, struct coord *pos)
618{
619 int next_row = pos->row;
620 pos->col = 0;
622 while (true) {
623 if (--next_row < 0)
624 return;
626 const struct row *row = grid_row_in_view(term->grid, next_row);
627 assert(row != NULL);
629 if (row->linebreak)
630 return;
632 pos->col = 0;
633 pos->row = next_row;
634 }
635}
637static void
638selection_find_line_boundary_right(struct terminal *term, struct coord *pos)
639{
640 int next_row = pos->row;
641 pos->col = term->cols - 1;
643 while (true) {
644 const struct row *row = grid_row_in_view(term->grid, next_row);
645 assert(row != NULL);
647 if (row->linebreak)
648 return;
650 if (++next_row >= term->rows)
651 return;
653 pos->col = term->cols - 1;
654 pos->row = next_row;
655 }
656}
658void
659selection_start(struct terminal *term, int col, int row,
660 enum selection_kind kind,
661 bool spaces_only)
662{
663 selection_cancel(term);
665 LOG_DBG("%s selection started at %d,%d",
666 kind == SELECTION_CHAR_WISE ? "character-wise" :
667 kind == SELECTION_WORD_WISE ? "word-wise" :
668 kind == SELECTION_QUOTE_WISE ? "quote-wise" :
669 kind == SELECTION_LINE_WISE ? "line-wise" :
670 kind == SELECTION_BLOCK ? "block" : "<unknown>",
671 row, col);
673 term->selection.kind = kind;
674 term->selection.ongoing = true;
675 term->selection.spaces_only = spaces_only;
677 switch (kind) {
678 case SELECTION_CHAR_WISE:
679 case SELECTION_BLOCK:
680 term->selection.coords.start = (struct coord){col, term->grid->view + row};
681 term->selection.coords.end = (struct coord){-1, -1};
683 term->selection.pivot.start = term->selection.coords.start;
684 term->selection.pivot.end = term->selection.coords.end;
685 break;
687 case SELECTION_WORD_WISE: {
688 struct coord start = {col, term->grid->view + row};
689 struct coord end = {col, term->grid->view + row};
690 selection_find_word_boundary_left(term, &start, spaces_only);
691 selection_find_word_boundary_right(term, &end, spaces_only, true);
693 term->selection.coords.start = start;
695 term->selection.pivot.start = term->selection.coords.start;
696 term->selection.pivot.end = end;
698 /*
699 * FIXME: go through selection.c and make sure all public
700 * functions use the *same* coordinate system...
701 *
702 * selection_find_word_boundary*() uses absolute row numbers,
703 * while selection_update(), and pretty much all others, use
704 * view-local.
705 */
707 selection_update(term, end.col, end.row - term->grid->view);
708 break;
709 }
711 case SELECTION_QUOTE_WISE: {
712 struct coord start = {col, row}, end = {col, row};
714 char32_t quote_char = '\0';
715 bool found_left = selection_find_quote_left(term, &start, "e_char);
716 bool found_right = selection_find_quote_right(term, &end, quote_char);
718 if (found_left && !found_right) {
719 xassert(quote_char != '\0');
721 /*
722 * Try to flip the quote character we're looking for.
723 *
724 * This lets us handle things like:
725 *
726 * "nested 'quotes are fun', right"
727 *
728 * In the example above, starting the selection at
729 * "right", will otherwise not match. find-left will find
730 * the single quote, causing find-right to fail.
731 *
732 * By flipping the quote-character, and re-trying, we
733 * find-left will find the starting double quote, letting
734 * find-right succeed as well.
735 */
737 if (quote_char == '\'')
738 quote_char = '"';
739 else if (quote_char == '"')
740 quote_char = '\'';
742 found_left = selection_find_quote_left(term, &start, "e_char);
743 found_right = selection_find_quote_right(term, &end, quote_char);
744 }
746 if (found_left && found_right) {
747 term->selection.coords.start = (struct coord){
748 start.col, term->grid->view + start.row};
750 term->selection.pivot.start = term->selection.coords.start;
751 term->selection.pivot.end = (struct coord){end.col, term->grid->view + end.row};
753 term->selection.kind = SELECTION_WORD_WISE;
754 selection_update(term, end.col, end.row);
755 break;
756 } else {
757 term->selection.kind = SELECTION_LINE_WISE;
758 /* FALLTHROUGH */
759 }
760 }
762 case SELECTION_LINE_WISE: {
763 struct coord start = {0, row}, end = {term->cols - 1, row};
764 selection_find_line_boundary_left(term, &start);
765 selection_find_line_boundary_right(term, &end);
767 term->selection.coords.start = (struct coord){
768 start.col, term->grid->view + start.row};
769 term->selection.pivot.start = term->selection.coords.start;
770 term->selection.pivot.end = (struct coord){end.col, term->grid->view + end.row};
772 selection_update(term, end.col, end.row);
773 break;
774 }
776 case SELECTION_NONE:
777 BUG("Invalid selection kind");
778 break;
779 }
781}
783static pixman_region32_t
784pixman_region_for_coords_normal(const struct terminal *term,
785 const struct coord *start,
786 const struct coord *end)
787{
788 pixman_region32_t region;
789 pixman_region32_init(®ion);
791 const int rel_start_row =
792 grid_row_abs_to_sb(term->grid, term->rows, start->row);
793 const int rel_end_row =
794 grid_row_abs_to_sb(term->grid, term->rows, end->row);
796 if (rel_start_row < rel_end_row) {
797 /* First partial row (start ->)*/
798 if (term->cols > start->col) {
799 pixman_region32_union_rect(
800 ®ion, ®ion,
801 start->col, rel_start_row,
802 term->cols - start->col, 1);
803 }
805 /* Full rows between start and end */
806 if (rel_start_row + 1 < rel_end_row) {
807 pixman_region32_union_rect(
808 ®ion, ®ion,
809 0, rel_start_row + 1,
810 term->cols, rel_end_row - rel_start_row - 1);
811 }
813 /* Last partial row (-> end) */
814 pixman_region32_union_rect(
815 ®ion, ®ion,
816 0, rel_end_row,
817 end->col + 1, 1);
819 } else if (rel_start_row > rel_end_row) {
820 /* First partial row (end ->) */
821 if (term->cols > end->col) {
822 pixman_region32_union_rect(
823 ®ion, ®ion,
824 end->col, rel_end_row,
825 term->cols - end->col, 1);
826 }
828 /* Full rows between end and start */
829 if (rel_end_row + 1 < rel_start_row) {
830 pixman_region32_union_rect(
831 ®ion, ®ion,
832 0, rel_end_row + 1,
833 term->cols, rel_start_row - rel_end_row - 1);
834 }
836 /* Last partial row (-> start) */
837 pixman_region32_union_rect(
838 ®ion, ®ion,
839 0, rel_start_row,
840 start->col + 1, 1);
841 } else {
842 const int start_col = min(start->col, end->col);
843 const int end_col = max(start->col, end->col);
845 pixman_region32_union_rect(
846 ®ion, ®ion,
847 start_col, rel_start_row,
848 end_col + 1 - start_col, 1);
849 }
851 return region;
852}
854static pixman_region32_t
855pixman_region_for_coords_block(const struct terminal *term,
856 const struct coord *start, const struct coord *end)
857{
858 pixman_region32_t region;
859 pixman_region32_init(®ion);
861 const int rel_start_row =
862 grid_row_abs_to_sb(term->grid, term->rows, start->row);
863 const int rel_end_row =
864 grid_row_abs_to_sb(term->grid, term->rows, end->row);
866 pixman_region32_union_rect(
867 ®ion, ®ion,
868 min(start->col, end->col), min(rel_start_row, rel_end_row),
869 abs(start->col - end->col) + 1, abs(rel_start_row - rel_end_row) + 1);
871 return region;
872}
874/* Returns a pixman region representing the selection between 'start'
875 * and 'end' (given the current selection kind), in *scrollback
876 * relative coordinates* */
877static pixman_region32_t
878pixman_region_for_coords(const struct terminal *term,
879 const struct coord *start, const struct coord *end)
880{
881 switch (term->selection.kind) {
882 default: return pixman_region_for_coords_normal(term, start, end);
883 case SELECTION_BLOCK: return pixman_region_for_coords_block(term, start, end);
884 }
885}
887enum mark_selection_variant {
888 MARK_SELECTION_MARK_AND_DIRTY,
889 MARK_SELECTION_UNMARK_AND_DIRTY,
890 MARK_SELECTION_MARK_FOR_RENDER,
891};
893static void
894mark_selected_region(struct terminal *term, pixman_box32_t *boxes,
895 size_t count, enum mark_selection_variant mark_variant)
896{
897 const bool selected =
898 mark_variant == MARK_SELECTION_MARK_AND_DIRTY ||
899 mark_variant == MARK_SELECTION_MARK_FOR_RENDER;
900 const bool dirty_cells =
901 mark_variant == MARK_SELECTION_MARK_AND_DIRTY ||
902 mark_variant == MARK_SELECTION_UNMARK_AND_DIRTY;
903 const bool highlight_empty =
904 mark_variant != MARK_SELECTION_MARK_FOR_RENDER ||
905 term->selection.kind == SELECTION_BLOCK;
907 for (size_t i = 0; i < count; i++) {
908 const pixman_box32_t *box = &boxes[i];
910 LOG_DBG("%s selection in region: %dx%d - %dx%d",
911 selected ? "marking" : "unmarking",
912 box->x1, box->y1,
913 box->x2, box->y2);
915 int abs_row_start = grid_row_sb_to_abs(
916 term->grid, term->rows, box->y1);
918 for (int r = abs_row_start, rel_r = box->y1;
919 rel_r < box->y2;
920 r = (r + 1) & (term->grid->num_rows - 1), rel_r++)
921 {
922 struct row *row = term->grid->rows[r];
923 xassert(row != NULL);
925 if (dirty_cells)
926 row->dirty = true;
928 for (int c = box->x1, empty_count = 0; c < box->x2; c++) {
929 struct cell *cell = &row->cells[c];
931 if ((cell->wc == 0 || cell->wc == CELL_SPACER) &&
932 !highlight_empty)
933 {
934 /*
935 * We used to highlight empty cells *if* they were
936 * followed by non-empty cell(s), since this
937 * corresponds to what gets extracted when the
938 * selection is copied (that is, empty cells
939 * "between" non-empty cells are converted to
940 * spaces).
941 *
942 * However, they way we handle selection updates
943 * (diffing the "old" selection area against the
944 * "new" one, using pixman regions), means we
945 * can't correctly update the state of empty
946 * cells. The result is "random" empty cells being
947 * rendered as selected when they shouldn't.
948 *
949 * "Fix" by *never* highlighting selected empty
950 * cells (they still get converted to spaces when
951 * copied, if followed by non-empty cells).
952 */
953 empty_count++;
955 /*
956 * When the selection is *modified*, empty cells
957 * are treated just like non-empty cells; they are
958 * marked as selected, and dirtied.
959 *
960 * This is due to how the algorithm for updating
961 * the selection works; it uses regions to
962 * calculate the difference between the "old" and
963 * the "new" selection. This makes it impossible
964 * to tell if an empty cell is a *trailing* empty
965 * cell (that should not be highlighted), or an
966 * empty cells between non-empty cells (that
967 * *should* be highlighted).
968 *
969 * Then, when a frame is rendered, we loop the
970 * *visibible* cells that belong to the
971 * selection. At this point, we *can* tell if an
972 * empty cell is trailing or not.
973 *
974 * So, what we need to do is check if a
975 * 'selected', and empty cell has been marked as
976 * selected, temporarily unmark (forcing it dirty,
977 * to ensure it gets re-rendered). If it is *not*
978 * a trailing empty cell, it will get re-tagged as
979 * selected in the for-loop below.
980 */
981 cell->attrs.clean = false;
982 cell->attrs.selected = false;
983 row->dirty = true;
984 continue;
985 }
987 for (int j = 0; j < empty_count + 1; j++) {
988 xassert(c - j >= 0);
989 struct cell *cell = &row->cells[c - j];
991 if (dirty_cells) {
992 cell->attrs.clean = false;
993 row->dirty = true;
994 }
995 cell->attrs.selected = selected;
996 }
998 empty_count = 0;
999 }
1000 }
1001 }
1002}
1004static void
1005selection_modify(struct terminal *term, struct coord start, struct coord end)
1006{
1007 xassert(term->selection.coords.start.row != -1);
1008 xassert(start.row != -1 && start.col != -1);
1009 xassert(end.row != -1 && end.col != -1);
1011 pixman_region32_t previous_selection;
1012 if (term->selection.coords.end.row >= 0) {
1013 previous_selection = pixman_region_for_coords(
1014 term,
1015 &term->selection.coords.start,
1016 &term->selection.coords.end);
1017 } else
1018 pixman_region32_init(&previous_selection);
1020 pixman_region32_t current_selection = pixman_region_for_coords(
1021 term, &start, &end);
1023 pixman_region32_t no_longer_selected;
1024 pixman_region32_init(&no_longer_selected);
1025 pixman_region32_subtract(
1026 &no_longer_selected, &previous_selection, ¤t_selection);
1028 pixman_region32_t newly_selected;
1029 pixman_region32_init(&newly_selected);
1030 pixman_region32_subtract(
1031 &newly_selected, ¤t_selection, &previous_selection);
1033 /* Clear selection in cells no longer selected */
1034 int n_rects = -1;
1035 pixman_box32_t *boxes = NULL;
1037 boxes = pixman_region32_rectangles(&no_longer_selected, &n_rects);
1038 mark_selected_region(term, boxes, n_rects, MARK_SELECTION_UNMARK_AND_DIRTY);
1040 boxes = pixman_region32_rectangles(&newly_selected, &n_rects);
1041 mark_selected_region(term, boxes, n_rects, MARK_SELECTION_MARK_AND_DIRTY);
1043 pixman_region32_fini(&newly_selected);
1044 pixman_region32_fini(&no_longer_selected);
1045 pixman_region32_fini(¤t_selection);
1046 pixman_region32_fini(&previous_selection);
1048 term->selection.coords.start = start;
1049 term->selection.coords.end = end;
1050 render_refresh(term);
1051}
1053static void
1054set_pivot_point_for_block_and_char_wise(struct terminal *term,
1055 struct coord start,
1056 enum selection_direction new_direction)
1057{
1058 struct coord *pivot_start = &term->selection.pivot.start;
1059 struct coord *pivot_end = &term->selection.pivot.end;
1061 *pivot_start = start;
1063 /* First, make sure 'start' isn't in the middle of a
1064 * multi-column character */
1065 while (true) {
1066 const struct row *row = term->grid->rows[pivot_start->row & (term->grid->num_rows - 1)];
1067 const struct cell *cell = &row->cells[pivot_start->col];
1069 if (cell->wc < CELL_SPACER)
1070 break;
1072 /* Multi-column chars don't cross rows */
1073 xassert(pivot_start->col > 0);
1074 if (pivot_start->col == 0)
1075 break;
1077 pivot_start->col--;
1078 }
1080 /*
1081 * Setup pivot end to be one character *before* start
1082 * Which one we move, the end or start point, depends
1083 * on the initial selection direction.
1084 */
1086 *pivot_end = *pivot_start;
1088 if (new_direction == SELECTION_RIGHT) {
1089 bool keep_going = true;
1090 while (keep_going) {
1091 const struct row *row = term->grid->rows[pivot_end->row & (term->grid->num_rows - 1)];
1092 const char32_t wc = row->cells[pivot_end->col].wc;
1094 keep_going = wc >= CELL_SPACER;
1096 if (pivot_end->col == 0) {
1097 if (pivot_end->row - term->grid->view <= 0)
1098 break;
1099 pivot_end->col = term->cols - 1;
1100 pivot_end->row--;
1101 } else
1102 pivot_end->col--;
1103 }
1104 } else {
1105 bool keep_going = true;
1106 while (keep_going) {
1107 const struct row *row = term->grid->rows[pivot_start->row & (term->grid->num_rows - 1)];
1108 const char32_t wc = pivot_start->col < term->cols - 1
1109 ? row->cells[pivot_start->col + 1].wc : 0;
1111 keep_going = wc >= CELL_SPACER;
1113 if (pivot_start->col >= term->cols - 1) {
1114 if (pivot_start->row - term->grid->view >= term->rows - 1)
1115 break;
1116 pivot_start->col = 0;
1117 pivot_start->row++;
1118 } else
1119 pivot_start->col++;
1120 }
1121 }
1123 xassert(term->grid->rows[pivot_start->row & (term->grid->num_rows - 1)]->
1124 cells[pivot_start->col].wc <= CELL_SPACER);
1125 xassert(term->grid->rows[pivot_end->row & (term->grid->num_rows - 1)]->
1126 cells[pivot_end->col].wc <= CELL_SPACER + 1);
1127}
1129void
1130selection_update(struct terminal *term, int col, int row)
1131{
1132 if (term->selection.coords.start.row < 0)
1133 return;
1135 if (!term->selection.ongoing)
1136 return;
1138 xassert(term->grid->view + row != -1);
1140 struct coord new_start = term->selection.coords.start;
1141 struct coord new_end = {col, term->grid->view + row};
1143 int rel_new_start_row = grid_row_abs_to_sb(term->grid, term->rows, new_start.row);
1144 int rel_new_end_row = grid_row_abs_to_sb(term->grid, term->rows, new_end.row);
1146 LOG_DBG("selection updated: start = %d,%d, end = %d,%d -> %d, %d",
1147 term->selection.coords.start.row, term->selection.coords.start.col,
1148 term->selection.coords.end.row, term->selection.coords.end.col,
1149 new_end.row, new_end.col);
1151 /* Adjust start point if the selection has changed 'direction' */
1152 if (!(rel_new_end_row == rel_new_start_row && new_end.col == new_start.col)) {
1153 enum selection_direction new_direction = term->selection.direction;
1155 struct coord *pivot_start = &term->selection.pivot.start;
1156 struct coord *pivot_end = &term->selection.pivot.end;
1158 int rel_pivot_start_row = grid_row_abs_to_sb(term->grid, term->rows, pivot_start->row);
1160 if (term->selection.kind == SELECTION_BLOCK) {
1161 if (new_end.col >= pivot_start->col)
1162 new_direction = SELECTION_RIGHT;
1163 else
1164 new_direction = SELECTION_LEFT;
1166 if (term->selection.direction == SELECTION_UNDIR)
1167 set_pivot_point_for_block_and_char_wise(term, *pivot_start, new_direction);
1169 if (new_direction == SELECTION_LEFT)
1170 new_start = *pivot_end;
1171 else
1172 new_start = *pivot_start;
1173 term->selection.direction = new_direction;
1174 } else {
1175 if (rel_new_end_row < rel_pivot_start_row ||
1176 (rel_new_end_row == rel_pivot_start_row &&
1177 new_end.col < pivot_start->col))
1178 {
1179 /* New end point is before the start point */
1180 new_direction = SELECTION_LEFT;
1181 } else {
1182 /* The new end point is after the start point */
1183 new_direction = SELECTION_RIGHT;
1184 }
1186 if (term->selection.direction != new_direction) {
1187 if (term->selection.direction == SELECTION_UNDIR &&
1188 pivot_end->row < 0)
1189 {
1190 set_pivot_point_for_block_and_char_wise(
1191 term, *pivot_start, new_direction);
1192 }
1194 if (new_direction == SELECTION_LEFT) {
1195 xassert(pivot_end->row >= 0);
1196 new_start = *pivot_end;
1197 } else
1198 new_start = *pivot_start;
1200 term->selection.direction = new_direction;
1201 }
1202 }
1203 }
1205 switch (term->selection.kind) {
1206 case SELECTION_CHAR_WISE:
1207 case SELECTION_BLOCK:
1208 break;
1210 case SELECTION_WORD_WISE:
1211 switch (term->selection.direction) {
1212 case SELECTION_LEFT:
1213 new_end = (struct coord){col, term->grid->view + row};
1214 selection_find_word_boundary_left(
1215 term, &new_end, term->selection.spaces_only);
1216 break;
1218 case SELECTION_RIGHT:
1219 new_end = (struct coord){col, term->grid->view + row};
1220 selection_find_word_boundary_right(
1221 term, &new_end, term->selection.spaces_only, true);
1222 break;
1224 case SELECTION_UNDIR:
1225 break;
1226 }
1227 break;
1229 case SELECTION_QUOTE_WISE:
1230 BUG("quote-wise selection should always be transformed to either word-wise or line-wise");
1231 break;
1233 case SELECTION_LINE_WISE:
1234 switch (term->selection.direction) {
1235 case SELECTION_LEFT: {
1236 struct coord end = {0, row};
1237 selection_find_line_boundary_left(term, &end);
1238 new_end = (struct coord){end.col, term->grid->view + end.row};
1239 break;
1240 }
1242 case SELECTION_RIGHT: {
1243 struct coord end = {col, row};
1244 selection_find_line_boundary_right(term, &end);
1245 new_end = (struct coord){end.col, term->grid->view + end.row};
1246 break;
1247 }
1249 case SELECTION_UNDIR:
1250 break;
1251 }
1252 break;
1254 case SELECTION_NONE:
1255 BUG("Invalid selection kind");
1256 break;
1257 }
1259 size_t start_row_idx = new_start.row & (term->grid->num_rows - 1);
1260 size_t end_row_idx = new_end.row & (term->grid->num_rows - 1);
1262 const struct row *row_start = term->grid->rows[start_row_idx];
1263 const struct row *row_end = term->grid->rows[end_row_idx];
1265 /*
1266 * Don't allow leading/trailing pad cells in the selection. Pull
1267 * the start/endpoint that is inside the pad region into the
1268 * multi-cell character, and then let the next step below move it
1269 * to the beginning of the multi-cell character.
1270 */
1271 while (unlikely(row_start->cells[new_start.col].wc == CELL_SPACER)) {
1272 new_start.col--;
1273 }
1274 while (unlikely(row_end->cells[new_end.col].wc == CELL_SPACER)) {
1275 new_end.col--;
1276 }
1278 /*
1279 * If an end point is in the middle of a multi-column character,
1280 * expand the selection to cover the entire character.
1281 *
1282 * Note that we check for CELL_SPACER+0 *inclusive* when backing
1283 * off. We do *not* want the point to be inside the pad area.
1284 *
1285 * But, we check for CELL_SPACER+0 *exclusive* when moving forward
1286 * _into_ the multi-cell character. Again, because we do *not*
1287 * want the point to be inside the pad area.
1288 */
1289 if (term->selection.direction != SELECTION_LEFT) {
1290 while (new_start.col >= 1 &&
1291 row_start->cells[new_start.col].wc >= CELL_SPACER)
1292 new_start.col--;
1293 while (new_end.col < term->cols - 1 &&
1294 row_end->cells[new_end.col + 1].wc > CELL_SPACER)
1295 new_end.col++;
1296 } else {
1297 while (new_end.col >= 1 &&
1298 row_end->cells[new_end.col].wc >= CELL_SPACER)
1299 new_end.col--;
1300 while (new_start.col < term->cols - 1 &&
1301 row_start->cells[new_start.col + 1].wc > CELL_SPACER)
1302 new_start.col++;
1303 }
1305 selection_modify(term, new_start, new_end);
1306}
1308void
1309selection_dirty_cells(struct terminal *term)
1310{
1311 if (term->selection.coords.start.row < 0 || term->selection.coords.end.row < 0)
1312 return;
1314 pixman_region32_t selection = pixman_region_for_coords(
1315 term, &term->selection.coords.start, &term->selection.coords.end);
1317 pixman_region32_t view = pixman_region_for_coords(
1318 term,
1319 &(struct coord){0, term->grid->view},
1320 &(struct coord){term->cols - 1, term->grid->view + term->rows - 1});
1322 pixman_region32_t visible_and_selected;
1323 pixman_region32_init(&visible_and_selected);
1324 pixman_region32_intersect(&visible_and_selected, &selection, &view);
1326 int n_rects = -1;
1327 pixman_box32_t *boxes =
1328 pixman_region32_rectangles(&visible_and_selected, &n_rects);
1329 mark_selected_region(term, boxes, n_rects, MARK_SELECTION_MARK_FOR_RENDER);
1331 pixman_region32_fini(&visible_and_selected);
1332 pixman_region32_fini(&view);
1333 pixman_region32_fini(&selection);
1334}
1336static void
1337selection_extend_normal(struct terminal *term, int col, int row,
1338 enum selection_kind new_kind)
1339{
1340 const struct coord *start = &term->selection.coords.start;
1341 const struct coord *end = &term->selection.coords.end;
1343 const int rel_row = grid_row_abs_to_sb(term->grid, term->rows, row);
1344 int rel_start_row = grid_row_abs_to_sb(term->grid, term->rows, start->row);
1345 int rel_end_row = grid_row_abs_to_sb(term->grid, term->rows, end->row);
1347 if (rel_start_row > rel_end_row ||
1348 (rel_start_row == rel_end_row && start->col > end->col))
1349 {
1350 const struct coord *tmp = start;
1351 start = end;
1352 end = tmp;
1354 int tmp_row = rel_start_row;
1355 rel_start_row = rel_end_row;
1356 rel_end_row = tmp_row;
1357 }
1359 struct coord new_start, new_end;
1360 enum selection_direction direction;
1362 if (rel_row < rel_start_row ||
1363 (rel_row == rel_start_row && col < start->col))
1364 {
1365 /* Extend selection to start *before* current start */
1366 new_start = *end;
1367 new_end = (struct coord){col, row};
1368 direction = SELECTION_LEFT;
1369 }
1371 else if (rel_row > rel_end_row ||
1372 (rel_row == rel_end_row && col > end->col))
1373 {
1374 /* Extend selection to end *after* current end */
1375 new_start = *start;
1376 new_end = (struct coord){col, row};
1377 direction = SELECTION_RIGHT;
1378 }
1380 else {
1381 /* Shrink selection from start or end, depending on which one is closest */
1383 const int linear = rel_row * term->cols + col;
1385 if (abs(linear - (rel_start_row * term->cols + start->col)) <
1386 abs(linear - (rel_end_row * term->cols + end->col)))
1387 {
1388 /* Move start point */
1389 new_start = *end;
1390 new_end = (struct coord){col, row};
1391 direction = SELECTION_LEFT;
1392 }
1394 else {
1395 /* Move end point */
1396 new_start = *start;
1397 new_end = (struct coord){col, row};
1398 direction = SELECTION_RIGHT;
1399 }
1400 }
1402 const bool spaces_only = term->selection.spaces_only;
1404 switch (term->selection.kind) {
1405 case SELECTION_CHAR_WISE:
1406 xassert(new_kind == SELECTION_CHAR_WISE);
1407 set_pivot_point_for_block_and_char_wise(term, new_start, direction);
1408 break;
1410 case SELECTION_WORD_WISE: {
1411 xassert(new_kind == SELECTION_CHAR_WISE ||
1412 new_kind == SELECTION_WORD_WISE);
1414 struct coord pivot_start = {new_start.col, new_start.row};
1415 struct coord pivot_end = pivot_start;
1417 selection_find_word_boundary_left(term, &pivot_start, spaces_only);
1418 selection_find_word_boundary_right(term, &pivot_end, spaces_only, true);
1420 term->selection.pivot.start = pivot_start;
1421 term->selection.pivot.end = pivot_end;
1422 break;
1423 }
1425 case SELECTION_QUOTE_WISE: {
1426 BUG("quote-wise selection should always be transformed to either word-wise or line-wise");
1427 break;
1428 }
1430 case SELECTION_LINE_WISE: {
1431 xassert(new_kind == SELECTION_CHAR_WISE ||
1432 new_kind == SELECTION_LINE_WISE);
1434 struct coord pivot_start = {new_start.col, new_start.row - term->grid->view};
1435 struct coord pivot_end = pivot_start;
1437 selection_find_line_boundary_left(term, &pivot_start);
1438 selection_find_line_boundary_right(term, &pivot_end);
1440 term->selection.pivot.start =
1441 (struct coord){pivot_start.col, term->grid->view + pivot_start.row};
1442 term->selection.pivot.end =
1443 (struct coord){pivot_end.col, term->grid->view + pivot_end.row};
1444 break;
1445 }
1447 case SELECTION_BLOCK:
1448 case SELECTION_NONE:
1449 BUG("Invalid selection kind in this context");
1450 break;
1451 }
1453 term->selection.kind = new_kind;
1454 term->selection.direction = direction;
1455 selection_modify(term, new_start, new_end);
1456}
1458static void
1459selection_extend_block(struct terminal *term, int col, int row)
1460{
1461 const struct coord *start = &term->selection.coords.start;
1462 const struct coord *end = &term->selection.coords.end;
1464 const int rel_start_row =
1465 grid_row_abs_to_sb(term->grid, term->rows, start->row);
1466 const int rel_end_row =
1467 grid_row_abs_to_sb(term->grid, term->rows, end->row);
1469 struct coord top_left = {
1470 .row = rel_start_row < rel_end_row ? start->row : end->row,
1471 .col = min(start->col, end->col),
1472 };
1474 struct coord top_right = {
1475 .row = top_left.row,
1476 .col = max(start->col, end->col),
1477 };
1479 struct coord bottom_left = {
1480 .row = rel_start_row > rel_end_row ? start->row : end->row,
1481 .col = min(start->col, end->col),
1482 };
1484 struct coord bottom_right = {
1485 .row = bottom_left.row,
1486 .col = max(start->col, end->col),
1487 };
1489 const int rel_row = grid_row_abs_to_sb(term->grid, term->rows, row);
1490 const int rel_top_row = grid_row_abs_to_sb(term->grid, term->rows, top_left.row);
1491 const int rel_bottom_row = grid_row_abs_to_sb(term->grid, term->rows, bottom_left.row);
1492 struct coord new_start;
1493 struct coord new_end;
1495 enum selection_direction direction = SELECTION_UNDIR;
1497 if (rel_row <= rel_top_row ||
1498 abs(rel_row - rel_top_row) < abs(rel_row - rel_bottom_row))
1499 {
1500 /* Move one of the top corners */
1502 if (abs(col - top_left.col) < abs(col - top_right.col)) {
1503 new_start = bottom_right;
1504 new_end = (struct coord){col, row};
1505 }
1507 else {
1508 new_start = bottom_left;
1509 new_end = (struct coord){col, row};
1510 }
1511 }
1513 else {
1514 /* Move one of the bottom corners */
1516 if (abs(col - bottom_left.col) < abs(col - bottom_right.col)) {
1517 new_start = top_right;
1518 new_end = (struct coord){col, row};
1519 }
1521 else {
1522 new_start = top_left;
1523 new_end = (struct coord){col, row};
1524 }
1525 }
1527 direction = col > new_start.col ? SELECTION_RIGHT : SELECTION_LEFT;
1528 set_pivot_point_for_block_and_char_wise(term, new_start, direction);
1530 term->selection.direction = direction;
1531 selection_modify(term, new_start, new_end);
1532}
1534void
1535selection_extend(struct seat *seat, struct terminal *term,
1536 int col, int row, enum selection_kind new_kind)
1537{
1538 if (term->selection.coords.start.row < 0 || term->selection.coords.end.row < 0) {
1539 /* No existing selection */
1540 return;
1541 }
1543 if (term->selection.kind == SELECTION_BLOCK && new_kind != SELECTION_BLOCK)
1544 return;
1546 term->selection.ongoing = true;
1548 row += term->grid->view;
1550 if ((row == term->selection.coords.start.row && col == term->selection.coords.start.col) ||
1551 (row == term->selection.coords.end.row && col == term->selection.coords.end.col))
1552 {
1553 /* Extension point *is* one of the current end points */
1554 return;
1555 }
1557 switch (term->selection.kind) {
1558 case SELECTION_NONE:
1559 BUG("Invalid selection kind");
1560 return;
1562 case SELECTION_CHAR_WISE:
1563 case SELECTION_WORD_WISE:
1564 case SELECTION_QUOTE_WISE:
1565 case SELECTION_LINE_WISE:
1566 selection_extend_normal(term, col, row, new_kind);
1567 break;
1569 case SELECTION_BLOCK:
1570 selection_extend_block(term, col, row);
1571 break;
1572 }
1573}
1575//static const struct zwp_primary_selection_source_v1_listener primary_selection_source_listener;
1577void
1578selection_finalize(struct seat *seat, struct terminal *term, uint32_t serial)
1579{
1580 if (!term->selection.ongoing)
1581 return;
1583 LOG_DBG("selection finalize");
1585 selection_stop_scroll_timer(term);
1586 term->selection.ongoing = false;
1588 if (term->selection.coords.start.row < 0 || term->selection.coords.end.row < 0)
1589 return;
1591 xassert(term->selection.coords.start.row != -1);
1592 xassert(term->selection.coords.end.row != -1);
1594 term->selection.coords.start.row &= (term->grid->num_rows - 1);
1595 term->selection.coords.end.row &= (term->grid->num_rows - 1);
1597 switch (term->conf->selection_target) {
1598 case SELECTION_TARGET_NONE:
1599 break;
1601 case SELECTION_TARGET_PRIMARY:
1602 selection_to_primary(seat, term, serial);
1603 break;
1604 case SELECTION_TARGET_CLIPBOARD:
1605 selection_to_clipboard(seat, term, serial);
1606 break;
1608 case SELECTION_TARGET_BOTH:
1609 selection_to_primary(seat, term, serial);
1610 selection_to_clipboard(seat, term, serial);
1611 break;
1612 }
1613}
1615static bool
1616unmark_selected(struct terminal *term, struct row *row, struct cell *cell,
1617 int row_no, int col, void *data)
1618{
1619 if (!cell->attrs.selected)
1620 return true;
1622 row->dirty = true;
1623 cell->attrs.selected = false;
1624 cell->attrs.clean = false;
1625 return true;
1626}
1628void
1629selection_cancel(struct terminal *term)
1630{
1631 LOG_DBG("selection cancelled: start = %d,%d end = %d,%d",
1632 term->selection.coords.start.row, term->selection.coords.start.col,
1633 term->selection.coords.end.row, term->selection.coords.end.col);
1635 selection_stop_scroll_timer(term);
1637 if (term->selection.coords.start.row >= 0 && term->selection.coords.end.row >= 0) {
1638 foreach_selected(
1639 term, term->selection.coords.start, term->selection.coords.end,
1640 &unmark_selected, NULL);
1641 render_refresh(term);
1642 }
1644 term->selection.kind = SELECTION_NONE;
1645 term->selection.coords.start = (struct coord){-1, -1};
1646 term->selection.coords.end = (struct coord){-1, -1};
1647 term->selection.pivot.start = (struct coord){-1, -1};
1648 term->selection.pivot.end = (struct coord){-1, -1};
1649 term->selection.direction = SELECTION_UNDIR;
1650 term->selection.ongoing = false;
1652 search_selection_cancelled(term);
1653}
1655bool
1656selection_clipboard_has_data(const struct seat *seat)
1657{
1658 return seat->clipboard.data_offer != NULL;
1659}
1661bool
1662selection_primary_has_data(const struct seat *seat)
1663{
1664 return seat->primary.data_offer != NULL;
1665}
1667void
1668selection_clipboard_unset(struct seat *seat)
1669{
1670 struct wl_clipboard *clipboard = &seat->clipboard;
1672 if (clipboard->data_source == NULL)
1673 return;
1675 /* Kill previous data source */
1676 xassert(clipboard->serial != 0);
1677 wl_data_device_set_selection(seat->data_device, NULL, clipboard->serial);
1678 wl_data_source_destroy(clipboard->data_source);
1680 clipboard->data_source = NULL;
1681 clipboard->serial = 0;
1683 free(clipboard->text);
1684 clipboard->text = NULL;
1685}
1687void
1688selection_primary_unset(struct seat *seat)
1689{
1690 struct wl_primary *primary = &seat->primary;
1692 if (primary->data_source == NULL)
1693 return;
1695 xassert(primary->serial != 0);
1696 zwp_primary_selection_device_v1_set_selection(
1697 seat->primary_selection_device, NULL, primary->serial);
1698 zwp_primary_selection_source_v1_destroy(primary->data_source);
1700 primary->data_source = NULL;
1701 primary->serial = 0;
1703 free(primary->text);
1704 primary->text = NULL;
1705}
1707static bool
1708fdm_scroll_timer(struct fdm *fdm, int fd, int events, void *data)
1709{
1710 if (events & EPOLLHUP)
1711 return false;
1713 struct terminal *term = data;
1715 uint64_t expiration_count;
1716 ssize_t ret = read(
1717 term->selection.auto_scroll.fd,
1718 &expiration_count, sizeof(expiration_count));
1720 if (ret < 0) {
1721 if (errno == EAGAIN)
1722 return true;
1724 LOG_ERRNO("failed to read selection scroll timer");
1725 return false;
1726 }
1728 switch (term->selection.auto_scroll.direction) {
1729 case SELECTION_SCROLL_NOT:
1730 return true;
1732 case SELECTION_SCROLL_UP:
1733 cmd_scrollback_up(term, expiration_count);
1734 selection_update(term, term->selection.auto_scroll.col, 0);
1735 break;
1737 case SELECTION_SCROLL_DOWN:
1738 cmd_scrollback_down(term, expiration_count);
1739 selection_update(term, term->selection.auto_scroll.col, term->rows - 1);
1740 break;
1741 }
1744 return true;
1745}
1747void
1748selection_start_scroll_timer(struct terminal *term, int interval_ns,
1749 enum selection_scroll_direction direction, int col)
1750{
1751 xassert(direction != SELECTION_SCROLL_NOT);
1753 if (!term->selection.ongoing)
1754 return;
1756 if (term->selection.auto_scroll.fd < 0) {
1757 int fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
1758 if (fd < 0) {
1759 LOG_ERRNO("failed to create selection scroll timer");
1760 goto err;
1761 }
1763 if (!fdm_add(term->fdm, fd, EPOLLIN, &fdm_scroll_timer, term)) {
1764 close(fd);
1765 return;
1766 }
1768 term->selection.auto_scroll.fd = fd;
1769 }
1771 struct itimerspec timer;
1772 if (timerfd_gettime(term->selection.auto_scroll.fd, &timer) < 0) {
1773 LOG_ERRNO("failed to get current selection scroll timer value");
1774 goto err;
1775 }
1777 if (timer.it_value.tv_sec == 0 && timer.it_value.tv_nsec == 0)
1778 timer.it_value.tv_nsec = 1;
1780 timer.it_interval.tv_sec = interval_ns / 1000000000;
1781 timer.it_interval.tv_nsec = interval_ns % 1000000000;
1783 if (timerfd_settime(term->selection.auto_scroll.fd, 0, &timer, NULL) < 0) {
1784 LOG_ERRNO("failed to set new selection scroll timer value");
1785 goto err;
1786 }
1788 term->selection.auto_scroll.direction = direction;
1789 term->selection.auto_scroll.col = col;
1790 return;
1792err:
1793 selection_stop_scroll_timer(term);
1794 return;
1795}
1797void
1798selection_stop_scroll_timer(struct terminal *term)
1799{
1800 if (term->selection.auto_scroll.fd < 0) {
1801 xassert(term->selection.auto_scroll.direction == SELECTION_SCROLL_NOT);
1802 return;
1803 }
1805 fdm_del(term->fdm, term->selection.auto_scroll.fd);
1806 term->selection.auto_scroll.fd = -1;
1807 term->selection.auto_scroll.direction = SELECTION_SCROLL_NOT;
1808}
1810static void
1811target(void *data, struct wl_data_source *wl_data_source, const char *mime_type)
1812{
1813 LOG_DBG("TARGET: mime-type=%s", mime_type);
1814}
1816struct clipboard_send {
1817 char *data;
1818 size_t len;
1819 size_t idx;
1820};
1822static bool
1823fdm_send(struct fdm *fdm, int fd, int events, void *data)
1824{
1825 struct clipboard_send *ctx = data;
1827 if (events & EPOLLHUP)
1828 goto done;
1830 switch (async_write(fd, ctx->data, ctx->len, &ctx->idx)) {
1831 case ASYNC_WRITE_REMAIN:
1832 return true;
1834 case ASYNC_WRITE_DONE:
1835 break;
1837 case ASYNC_WRITE_ERR:
1838 LOG_ERRNO(
1839 "failed to asynchronously write %zu of selection data to FD=%d",
1840 ctx->len - ctx->idx, fd);
1841 break;
1842 }
1844done:
1845 fdm_del(fdm, fd);
1846 free(ctx->data);
1847 free(ctx);
1848 return true;
1849}
1851static void
1852send_clipboard_or_primary(struct seat *seat, int fd, const char *selection,
1853 const char *source_name)
1854{
1855 /* Make it NONBLOCK:ing right away - we don't want to block if the
1856 * initial attempt to send the data synchronously fails */
1857 int flags;
1858 if ((flags = fcntl(fd, F_GETFL)) < 0 ||
1859 fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
1860 {
1861 LOG_ERRNO("failed to set O_NONBLOCK");
1862 return;
1863 }
1865 size_t len = selection != NULL ? strlen(selection) : 0;
1866 size_t async_idx = 0;
1868 switch (async_write(fd, selection, len, &async_idx)) {
1869 case ASYNC_WRITE_REMAIN: {
1870 struct clipboard_send *ctx = xmalloc(sizeof(*ctx));
1871 *ctx = (struct clipboard_send) {
1872 .data = xstrdup(&selection[async_idx]),
1873 .len = len - async_idx,
1874 .idx = 0,
1875 };
1877 if (fdm_add(seat->wayl->fdm, fd, EPOLLOUT, &fdm_send, ctx))
1878 return;
1880 free(ctx->data);
1881 free(ctx);
1882 break;
1883 }
1885 case ASYNC_WRITE_DONE:
1886 break;
1888 case ASYNC_WRITE_ERR:
1889 LOG_ERRNO("failed write %zu bytes of %s selection data to FD=%d",
1890 len, source_name, fd);
1891 break;
1892 }
1894 close(fd);
1895}
1897static void
1898send(void *data, struct wl_data_source *wl_data_source, const char *mime_type,
1899 int32_t fd)
1900{
1901 struct seat *seat = data;
1902 const struct wl_clipboard *clipboard = &seat->clipboard;
1904 send_clipboard_or_primary(seat, fd, clipboard->text, "clipboard");
1905}
1907static void
1908cancelled(void *data, struct wl_data_source *wl_data_source)
1909{
1910 struct seat *seat = data;
1911 struct wl_clipboard *clipboard = &seat->clipboard;
1912 xassert(clipboard->data_source == wl_data_source);
1914 wl_data_source_destroy(clipboard->data_source);
1915 clipboard->data_source = NULL;
1916 clipboard->serial = 0;
1918 free(clipboard->text);
1919 clipboard->text = NULL;
1920}
1922/* We don't support dragging *from* */
1923static void
1924dnd_drop_performed(void *data, struct wl_data_source *wl_data_source)
1925{
1926 //LOG_DBG("DnD drop performed");
1927}
1929static void
1930dnd_finished(void *data, struct wl_data_source *wl_data_source)
1931{
1932 //LOG_DBG("DnD finished");
1933}
1935static void
1936action(void *data, struct wl_data_source *wl_data_source, uint32_t dnd_action)
1937{
1938 //LOG_DBG("DnD action: %u", dnd_action);
1939}
1941static const struct wl_data_source_listener data_source_listener = {
1942 .target = &target,
1943 .send = &send,
1944 .cancelled = &cancelled,
1945 .dnd_drop_performed = &dnd_drop_performed,
1946 .dnd_finished = &dnd_finished,
1947 .action = &action,
1948};
1950static void
1951primary_send(void *data,
1952 struct zwp_primary_selection_source_v1 *zwp_primary_selection_source_v1,
1953 const char *mime_type, int32_t fd)
1954{
1955 struct seat *seat = data;
1956 const struct wl_primary *primary = &seat->primary;
1958 send_clipboard_or_primary(seat, fd, primary->text, "primary");
1959}
1961static void
1962primary_cancelled(void *data,
1963 struct zwp_primary_selection_source_v1 *zwp_primary_selection_source_v1)
1964{
1965 struct seat *seat = data;
1966 struct wl_primary *primary = &seat->primary;
1968 zwp_primary_selection_source_v1_destroy(primary->data_source);
1969 primary->data_source = NULL;
1970 primary->serial = 0;
1972 free(primary->text);
1973 primary->text = NULL;
1974}
1976static const struct zwp_primary_selection_source_v1_listener primary_selection_source_listener = {
1977 .send = &primary_send,
1978 .cancelled = &primary_cancelled,
1979};
1981bool
1982text_to_clipboard(struct seat *seat, struct terminal *term, char *text, uint32_t serial)
1983{
1984 if (text == NULL || text[0] == '\0')
1985 return false;
1987 xassert(serial != 0);
1989 struct wl_clipboard *clipboard = &seat->clipboard;
1991 if (clipboard->data_source != NULL) {
1992 /* Kill previous data source */
1993 xassert(clipboard->serial != 0);
1994 wl_data_device_set_selection(seat->data_device, NULL, clipboard->serial);
1995 wl_data_source_destroy(clipboard->data_source);
1996 free(clipboard->text);
1998 clipboard->data_source = NULL;
1999 clipboard->serial = 0;
2000 clipboard->text = NULL;
2001 }
2003 clipboard->data_source
2004 = wl_data_device_manager_create_data_source(term->wl->data_device_manager);
2006 if (clipboard->data_source == NULL) {
2007 LOG_ERR("failed to create clipboard data source");
2008 return false;
2009 }
2011 clipboard->text = text;
2013 /* Configure source */
2014 wl_data_source_offer(clipboard->data_source, mime_type_map[DATA_OFFER_MIME_TEXT_UTF8]);
2015 wl_data_source_offer(clipboard->data_source, mime_type_map[DATA_OFFER_MIME_TEXT_PLAIN]);
2016 wl_data_source_offer(clipboard->data_source, mime_type_map[DATA_OFFER_MIME_TEXT_TEXT]);;
2017 wl_data_source_offer(clipboard->data_source, mime_type_map[DATA_OFFER_MIME_TEXT_STRING]);
2018 wl_data_source_offer(clipboard->data_source, mime_type_map[DATA_OFFER_MIME_TEXT_UTF8_STRING]);
2020 wl_data_source_add_listener(clipboard->data_source, &data_source_listener, seat);
2021 wl_data_device_set_selection(seat->data_device, clipboard->data_source, serial);
2023 /* Needed when sending the selection to other client */
2024 clipboard->serial = serial;
2025 return true;
2026}
2028void
2029selection_to_clipboard(struct seat *seat, struct terminal *term, uint32_t serial)
2030{
2031 if (term->selection.coords.start.row < 0 || term->selection.coords.end.row < 0)
2032 return;
2034 /* Get selection as a string */
2035 char *text = selection_to_text(term);
2036 if (!text_to_clipboard(seat, term, text, serial))
2037 free(text);
2038}
2040struct clipboard_receive {
2041 int read_fd;
2042 int timeout_fd;
2043 struct itimerspec timeout;
2044 bool bracketed;
2045 bool no_strip;
2046 bool quote_paths;
2048 void (*decoder)(struct clipboard_receive *ctx, char *data, size_t size);
2049 void (*finish)(struct clipboard_receive *ctx);
2051 /* URI state */
2052 bool add_space;
2053 struct {
2054 char *data;
2055 size_t sz;
2056 size_t idx;
2057 } buf;
2059 /* Callback data */
2060 void (*cb)(char *data, size_t size, void *user);
2061 void (*done)(void *user);
2062 void *user;
2063};
2065static void
2066clipboard_receive_done(struct fdm *fdm, struct clipboard_receive *ctx)
2067{
2068 fdm_del(fdm, ctx->timeout_fd);
2069 fdm_del(fdm, ctx->read_fd);
2070 ctx->done(ctx->user);
2071 free(ctx->buf.data);
2072 free(ctx);
2073}
2075static bool
2076fdm_receive_timeout(struct fdm *fdm, int fd, int events, void *data)
2077{
2078 struct clipboard_receive *ctx = data;
2079 if (events & EPOLLHUP)
2080 return false;
2082 xassert(events & EPOLLIN);
2084 uint64_t expire_count;
2085 ssize_t ret = read(fd, &expire_count, sizeof(expire_count));
2086 if (ret < 0) {
2087 if (errno == EAGAIN)
2088 return true;
2090 LOG_ERRNO("failed to read clipboard timeout timer");
2091 return false;
2092 }
2094 LOG_WARN("no data received from clipboard in %llu seconds, aborting",
2095 (unsigned long long)ctx->timeout.it_value.tv_sec);
2097 clipboard_receive_done(fdm, ctx);
2098 return true;
2099}
2101static void
2102fdm_receive_decoder_plain(struct clipboard_receive *ctx, char *data, size_t size)
2103{
2104 ctx->cb(data, size, ctx->user);
2105}
2107static void
2108fdm_receive_finish_plain(struct clipboard_receive *ctx)
2109{
2110}
2112static bool
2113decode_one_uri(struct clipboard_receive *ctx, char *uri, size_t len)
2114{
2115 LOG_DBG("URI: \"%.*s\"", (int)len, uri);
2117 if (len == 0)
2118 return false;
2120 char *scheme, *host, *path;
2121 if (!uri_parse(uri, len, &scheme, NULL, NULL, &host, NULL, &path, NULL, NULL)) {
2122 LOG_ERR("drag-and-drop: invalid URI: %.*s", (int)len, uri);
2123 return false;
2124 }
2126 if (ctx->add_space)
2127 ctx->cb(" ", 1, ctx->user);
2128 ctx->add_space = true;
2130 if (streq(scheme, "file") && hostname_is_localhost(host)) {
2131 if (ctx->quote_paths)
2132 ctx->cb("'", 1, ctx->user);
2134 char *path_remaining = path;
2135 for (char *next_quote = strchr(path_remaining, '\'');
2136 next_quote != NULL;
2137 path_remaining = next_quote + 1,
2138 next_quote = strchr(path_remaining, '\''))
2139 {
2140 ctx->cb(path_remaining, next_quote - path_remaining, ctx->user);
2141 ctx->cb("\\'", 2, ctx->user);
2142 }
2143 ctx->cb(path_remaining, strlen(path_remaining), ctx->user);
2145 if (ctx->quote_paths)
2146 ctx->cb("'", 1, ctx->user);
2147 } else
2148 ctx->cb(uri, len, ctx->user);
2150 free(scheme);
2151 free(host);
2152 free(path);
2153 return true;
2154}
2156static void
2157fdm_receive_decoder_uri(struct clipboard_receive *ctx, char *data, size_t size)
2158{
2159 while (ctx->buf.idx + size > ctx->buf.sz) {
2160 size_t new_sz = ctx->buf.sz == 0 ? size : 2 * ctx->buf.sz;
2161 ctx->buf.data = xrealloc(ctx->buf.data, new_sz);
2162 ctx->buf.sz = new_sz;
2163 }
2165 memcpy(&ctx->buf.data[ctx->buf.idx], data, size);
2166 ctx->buf.idx += size;
2168 char *start = ctx->buf.data;
2169 char *end = NULL;
2171 while (true) {
2172 for (end = start; end < &ctx->buf.data[ctx->buf.idx]; end++) {
2173 if (*end == '\r' || *end == '\n')
2174 break;
2175 }
2177 if (end >= &ctx->buf.data[ctx->buf.idx])
2178 break;
2180 decode_one_uri(ctx, start, end - start);
2181 start = end + 1;
2182 }
2184 const size_t ofs = start - ctx->buf.data;
2185 const size_t left = ctx->buf.idx - ofs;
2187 memmove(&ctx->buf.data[0], &ctx->buf.data[ofs], left);
2188 ctx->buf.idx = left;
2189}
2191static void
2192fdm_receive_finish_uri(struct clipboard_receive *ctx)
2193{
2194 LOG_DBG("finish: %.*s", (int)ctx->buf.idx, ctx->buf.data);
2195 decode_one_uri(ctx, ctx->buf.data, ctx->buf.idx);
2196}
2198static bool
2199fdm_receive(struct fdm *fdm, int fd, int events, void *data)
2200{
2201 struct clipboard_receive *ctx = data;
2202 const bool no_strip = ctx->no_strip;
2203 const bool bracketed = ctx->bracketed;
2205 if ((events & EPOLLHUP) && !(events & EPOLLIN))
2206 goto done;
2208 /* Reset timeout timer */
2209 if (timerfd_settime(ctx->timeout_fd, 0, &ctx->timeout, NULL) < 0) {
2210 LOG_ERRNO("failed to re-arm clipboard timeout timer");
2211 return false;
2212 }
2214 /* Read until EOF */
2215 while (true) {
2216 char text[256];
2217 ssize_t count = read(fd, text, sizeof(text));
2219 if (count == -1) {
2220 if (errno == EAGAIN || errno == EWOULDBLOCK)
2221 return true;
2223 LOG_ERRNO("failed to read clipboard data");
2224 break;
2225 }
2227 if (count == 0)
2228 break;
2230 /*
2231 * Call cb while at same time replace:
2232 * - \r\n -> \r (non-bracketed paste)
2233 * - \n -> \r (non-bracketed paste)
2234 * - C0 -> <nothing> (strip non-formatting C0 characters)
2235 * - \e -> <nothing> (i.e. strip ESC)
2236 */
2237 char *p = text;
2238 size_t left = count;
2240#define skip_one() \
2241 do { \
2242 ctx->decoder(ctx, p, i); \
2243 xassert(i + 1 <= left); \
2244 p += i + 1; \
2245 left -= i + 1; \
2246 } while (0)
2248 again:
2249 for (size_t i = 0; i < left; i++) {
2250 switch (p[i]) {
2251 default:
2252 break;
2254 case '\n':
2255 if (!no_strip && !bracketed) {
2256 p[i] = '\r';
2257 }
2258 break;
2260 case '\r':
2261 /* Convert \r\n -> \r */
2262 if (!no_strip && !bracketed && i + 1 < left && p[i + 1] == '\n') {
2263 i++;
2264 skip_one();
2265 goto again;
2266 }
2267 break;
2269 /* C0 non-formatting control characters (\b \t \n \r excluded) */
2270 case '\x01': case '\x02': case '\x03': case '\x04': case '\x05':
2271 case '\x06': case '\x07': case '\x0e': case '\x0f': case '\x10':
2272 case '\x11': case '\x12': case '\x13': case '\x14': case '\x15':
2273 case '\x16': case '\x17': case '\x18': case '\x19': case '\x1a':
2274 case '\x1b': case '\x1c': case '\x1d': case '\x1e': case '\x1f':
2275 if (!no_strip) {
2276 skip_one();
2277 goto again;
2278 }
2279 break;
2281 /*
2282 * In addition to stripping non-formatting C0 controls,
2283 * XTerm has an option, "disallowedPasteControls", that
2284 * defines C0 controls that will be replaced with spaces
2285 * when pasted.
2286 *
2287 * It's default value is BS,DEL,ENQ,EOT,NUL
2288 *
2289 * Instead of replacing them with spaces, we allow them in
2290 * bracketed paste mode, and strip them completely in
2291 * non-bracketed mode.
2292 *
2293 * Note some of the (default) XTerm controls are already
2294 * handled above.
2295 */
2296 case '\b': case '\x7f': case '\x00':
2297 if (!no_strip && !bracketed) {
2298 skip_one();
2299 goto again;
2300 }
2301 break;
2302 }
2303 }
2305 ctx->decoder(ctx, p, left);
2306 left = 0;
2307 }
2309#undef skip_one
2311done:
2312 ctx->finish(ctx);
2313 clipboard_receive_done(fdm, ctx);
2314 return true;
2315}
2317static void
2318begin_receive_clipboard(struct terminal *term, bool no_strip,
2319 int read_fd, enum data_offer_mime_type mime_type,
2320 void (*cb)(char *data, size_t size, void *user),
2321 void (*done)(void *user), void *user)
2322{
2323 int timeout_fd = -1;
2324 struct clipboard_receive *ctx = NULL;
2326 int flags;
2327 if ((flags = fcntl(read_fd, F_GETFL)) < 0 ||
2328 fcntl(read_fd, F_SETFL, flags | O_NONBLOCK) < 0)
2329 {
2330 LOG_ERRNO("failed to set O_NONBLOCK");
2331 goto err;
2332 }
2334 timeout_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
2335 if (timeout_fd < 0) {
2336 LOG_ERRNO("failed to create clipboard timeout timer FD");
2337 goto err;
2338 }
2340 const struct itimerspec timeout = {.it_value = {.tv_sec = 2}};
2341 if (timerfd_settime(timeout_fd, 0, &timeout, NULL) < 0) {
2342 LOG_ERRNO("failed to arm clipboard timeout timer");
2343 goto err;
2344 }
2346 ctx = xmalloc(sizeof(*ctx));
2347 *ctx = (struct clipboard_receive) {
2348 .read_fd = read_fd,
2349 .timeout_fd = timeout_fd,
2350 .timeout = timeout,
2351 .bracketed = term->bracketed_paste,
2352 .no_strip = no_strip,
2353 .quote_paths = term->grid == &term->normal,
2354 .decoder = (mime_type == DATA_OFFER_MIME_URI_LIST
2355 ? &fdm_receive_decoder_uri
2356 : &fdm_receive_decoder_plain),
2357 .finish = (mime_type == DATA_OFFER_MIME_URI_LIST
2358 ? &fdm_receive_finish_uri
2359 : &fdm_receive_finish_plain),
2360 .cb = cb,
2361 .done = done,
2362 .user = user,
2363 };
2365 if (!fdm_add(term->fdm, read_fd, EPOLLIN, &fdm_receive, ctx) ||
2366 !fdm_add(term->fdm, timeout_fd, EPOLLIN, &fdm_receive_timeout, ctx))
2367 {
2368 goto err;
2369 }
2371 return;
2373err:
2374 free(ctx);
2375 fdm_del(term->fdm, timeout_fd);
2376 fdm_del(term->fdm, read_fd);
2377 done(user);
2378}
2380void
2381text_from_clipboard(struct seat *seat, struct terminal *term,
2382 bool no_strip,
2383 void (*cb)(char *data, size_t size, void *user),
2384 void (*done)(void *user), void *user)
2385{
2386 struct wl_clipboard *clipboard = &seat->clipboard;
2387 if (clipboard->data_offer == NULL ||
2388 clipboard->mime_type == DATA_OFFER_MIME_UNSET)
2389 {
2390 done(user);
2391 return;
2392 }
2394 /* Prepare a pipe the other client can write its selection to us */
2395 int fds[2];
2396 if (pipe2(fds, O_CLOEXEC) == -1) {
2397 LOG_ERRNO("failed to create pipe");
2398 done(user);
2399 return;
2400 }
2402 LOG_DBG("receive from clipboard: mime-type=%s",
2403 mime_type_map[clipboard->mime_type]);
2405 int read_fd = fds[0];
2406 int write_fd = fds[1];
2408 /* Give write-end of pipe to other client */
2409 wl_data_offer_receive(
2410 clipboard->data_offer, mime_type_map[clipboard->mime_type], write_fd);
2412 /* Don't keep our copy of the write-end open (or we'll never get EOF) */
2413 close(write_fd);
2415 begin_receive_clipboard(
2416 term, no_strip, read_fd, clipboard->mime_type, cb, done, user);
2417}
2419static void
2420receive_offer(char *data, size_t size, void *user)
2421{
2422 struct terminal *term = user;
2423 xassert(term->is_sending_paste_data);
2424 term_paste_data_to_slave(term, data, size);
2425}
2427static void
2428receive_offer_done(void *user)
2429{
2430 struct terminal *term = user;
2432 if (term->bracketed_paste)
2433 term_paste_data_to_slave(term, "\033[201~", 6);
2435 term->is_sending_paste_data = false;
2437 /* Make sure we send any queued up non-paste data */
2438 if (tll_length(term->ptmx_buffers) > 0)
2439 fdm_event_add(term->fdm, term->ptmx, EPOLLOUT);
2440}
2442void
2443selection_from_clipboard(struct seat *seat, struct terminal *term, uint32_t serial)
2444{
2445 if (term->is_sending_paste_data) {
2446 /* We're already pasting... */
2447 return;
2448 }
2450 struct wl_clipboard *clipboard = &seat->clipboard;
2451 if (clipboard->data_offer == NULL)
2452 return;
2454 term->is_sending_paste_data = true;
2456 if (term->bracketed_paste)
2457 term_paste_data_to_slave(term, "\033[200~", 6);
2459 text_from_clipboard(
2460 seat, term, false, &receive_offer, &receive_offer_done, term);
2461}
2463bool
2464text_to_primary(struct seat *seat, struct terminal *term, char *text, uint32_t serial)
2465{
2466 if (text == NULL || text[0] == '\0')
2467 return false;
2469 if (term->wl->primary_selection_device_manager == NULL)
2470 return false;
2472 xassert(serial != 0);
2474 struct wl_primary *primary = &seat->primary;
2476 /* TODO: somehow share code with the clipboard equivalent */
2477 if (seat->primary.data_source != NULL) {
2478 /* Kill previous data source */
2480 xassert(primary->serial != 0);
2481 zwp_primary_selection_device_v1_set_selection(
2482 seat->primary_selection_device, NULL, primary->serial);
2483 zwp_primary_selection_source_v1_destroy(primary->data_source);
2484 free(primary->text);
2486 primary->data_source = NULL;
2487 primary->serial = 0;
2488 primary->text = NULL;
2489 }
2491 primary->data_source
2492 = zwp_primary_selection_device_manager_v1_create_source(
2493 term->wl->primary_selection_device_manager);
2495 if (primary->data_source == NULL) {
2496 LOG_ERR("failed to create clipboard data source");
2497 return false;
2498 }
2500 /* Get selection as a string */
2501 primary->text = text;
2503 /* Configure source */
2504 zwp_primary_selection_source_v1_offer(primary->data_source, mime_type_map[DATA_OFFER_MIME_TEXT_UTF8]);
2505 zwp_primary_selection_source_v1_offer(primary->data_source, mime_type_map[DATA_OFFER_MIME_TEXT_PLAIN]);
2506 zwp_primary_selection_source_v1_offer(primary->data_source, mime_type_map[DATA_OFFER_MIME_TEXT_TEXT]);
2507 zwp_primary_selection_source_v1_offer(primary->data_source, mime_type_map[DATA_OFFER_MIME_TEXT_STRING]);
2508 zwp_primary_selection_source_v1_offer(primary->data_source, mime_type_map[DATA_OFFER_MIME_TEXT_UTF8_STRING]);
2510 zwp_primary_selection_source_v1_add_listener(primary->data_source, &primary_selection_source_listener, seat);
2511 zwp_primary_selection_device_v1_set_selection(seat->primary_selection_device, primary->data_source, serial);
2513 /* Needed when sending the selection to other client */
2514 primary->serial = serial;
2515 return true;
2516}
2518void
2519selection_to_primary(struct seat *seat, struct terminal *term, uint32_t serial)
2520{
2521 if (term->wl->primary_selection_device_manager == NULL)
2522 return;
2524 /* Get selection as a string */
2525 char *text = selection_to_text(term);
2526 if (!text_to_primary(seat, term, text, serial))
2527 free(text);
2528}
2530void
2531text_from_primary(
2532 struct seat *seat, struct terminal *term, bool no_strip,
2533 void (*cb)(char *data, size_t size, void *user),
2534 void (*done)(void *user), void *user)
2535{
2536 if (term->wl->primary_selection_device_manager == NULL) {
2537 done(user);
2538 return;
2539 }
2541 struct wl_primary *primary = &seat->primary;
2542 if (primary->data_offer == NULL ||
2543 primary->mime_type == DATA_OFFER_MIME_UNSET)
2544 {
2545 done(user);
2546 return;
2547 }
2549 /* Prepare a pipe the other client can write its selection to us */
2550 int fds[2];
2551 if (pipe2(fds, O_CLOEXEC) == -1) {
2552 LOG_ERRNO("failed to create pipe");
2553 done(user);
2554 return;
2555 }
2557 LOG_DBG("receive from primary: mime-type=%s",
2558 mime_type_map[primary->mime_type]);
2560 int read_fd = fds[0];
2561 int write_fd = fds[1];
2563 /* Give write-end of pipe to other client */
2564 zwp_primary_selection_offer_v1_receive(
2565 primary->data_offer, mime_type_map[primary->mime_type], write_fd);
2567 /* Don't keep our copy of the write-end open (or we'll never get EOF) */
2568 close(write_fd);
2570 begin_receive_clipboard(
2571 term, no_strip, read_fd, primary->mime_type, cb, done, user);
2572}
2574void
2575selection_from_primary(struct seat *seat, struct terminal *term)
2576{
2577 if (term->wl->primary_selection_device_manager == NULL)
2578 return;
2580 if (term->is_sending_paste_data) {
2581 /* We're already pasting... */
2582 return;
2583 }
2585 struct wl_primary *primary = &seat->primary;
2586 if (primary->data_offer == NULL)
2587 return;
2589 term->is_sending_paste_data = true;
2590 if (term->bracketed_paste)
2591 term_paste_data_to_slave(term, "\033[200~", 6);
2593 text_from_primary(
2594 seat, term, false, &receive_offer, &receive_offer_done, term);
2595}
2597static void
2598select_mime_type_for_offer(const char *_mime_type,
2599 enum data_offer_mime_type *type)
2600{
2601 enum data_offer_mime_type mime_type = DATA_OFFER_MIME_UNSET;
2603 /* Translate offered mime type to our mime type enum */
2604 for (size_t i = 0; i < ALEN(mime_type_map); i++) {
2605 if (mime_type_map[i] == NULL)
2606 continue;
2608 if (streq(_mime_type, mime_type_map[i])) {
2609 mime_type = i;
2610 break;
2611 }
2612 }
2614 LOG_DBG("mime-type: %s -> %s (offered type was %s)",
2615 mime_type_map[*type], mime_type_map[mime_type], _mime_type);
2617 /* Mime-type transition; if the new mime-type is "better" than
2618 * previously offered types, use the new type */
2620 switch (mime_type) {
2621 case DATA_OFFER_MIME_TEXT_PLAIN:
2622 case DATA_OFFER_MIME_TEXT_TEXT:
2623 case DATA_OFFER_MIME_TEXT_STRING:
2624 /* text/plain is our least preferred type. Only use if current
2625 * type is unset */
2626 switch (*type) {
2627 case DATA_OFFER_MIME_UNSET:
2628 *type = mime_type;
2629 break;
2631 default:
2632 break;
2633 }
2634 break;
2636 case DATA_OFFER_MIME_TEXT_UTF8:
2637 case DATA_OFFER_MIME_TEXT_UTF8_STRING:
2638 /* text/plain;charset=utf-8 is preferred over text/plain */
2639 switch (*type) {
2640 case DATA_OFFER_MIME_UNSET:
2641 case DATA_OFFER_MIME_TEXT_PLAIN:
2642 case DATA_OFFER_MIME_TEXT_TEXT:
2643 case DATA_OFFER_MIME_TEXT_STRING:
2644 *type = mime_type;
2645 break;
2647 default:
2648 break;
2649 }
2650 break;
2652 case DATA_OFFER_MIME_URI_LIST:
2653 /* text/uri-list is always used when offered */
2654 *type = mime_type;
2655 break;
2657 case DATA_OFFER_MIME_UNSET:
2658 break;
2659 }
2660}
2662static void
2663data_offer_reset(struct wl_clipboard *clipboard)
2664{
2665 if (clipboard->data_offer != NULL) {
2666 wl_data_offer_destroy(clipboard->data_offer);
2667 clipboard->data_offer = NULL;
2668 }
2670 clipboard->window = NULL;
2671 clipboard->mime_type = DATA_OFFER_MIME_UNSET;
2672}
2674static void
2675offer(void *data, struct wl_data_offer *wl_data_offer, const char *mime_type)
2676{
2677 struct seat *seat = data;
2678 select_mime_type_for_offer(mime_type, &seat->clipboard.mime_type);
2679}
2681static void
2682source_actions(void *data, struct wl_data_offer *wl_data_offer,
2683 uint32_t source_actions)
2684{
2685#if defined(_DEBUG) && LOG_ENABLE_DBG
2686 char actions_as_string[1024];
2687 size_t idx = 0;
2689 actions_as_string[0] = '\0';
2690 actions_as_string[sizeof(actions_as_string) - 1] = '\0';
2692 for (size_t i = 0; i < 31; i++) {
2693 if (((source_actions >> i) & 1) == 0)
2694 continue;
2696 enum wl_data_device_manager_dnd_action action = 1 << i;
2698 const char *s = NULL;
2700 switch (action) {
2701 case WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE: s = NULL; break;
2702 case WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY: s = "copy"; break;
2703 case WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE: s = "move"; break;
2704 case WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK: s = "ask"; break;
2705 }
2707 if (s == NULL)
2708 continue;
2710 strncat(actions_as_string, s, sizeof(actions_as_string) - idx - 1);
2711 idx += strlen(s);
2712 strncat(actions_as_string, ", ", sizeof(actions_as_string) - idx - 1);
2713 idx += 2;
2714 }
2716 /* Strip trailing ", " */
2717 if (strlen(actions_as_string) > 2)
2718 actions_as_string[strlen(actions_as_string) - 2] = '\0';
2720 LOG_DBG("DnD actions: %s (0x%08x)", actions_as_string, source_actions);
2721#endif
2722}
2724static void
2725offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
2726{
2727#if defined(_DEBUG) && LOG_ENABLE_DBG
2728 const char *s = NULL;
2730 switch (dnd_action) {
2731 case WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE: s = "<none>"; break;
2732 case WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY: s = "copy"; break;
2733 case WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE: s = "move"; break;
2734 case WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK: s = "ask"; break;
2735 }
2737 LOG_DBG("DnD offer action: %s (0x%08x)", s, dnd_action);
2738#endif
2739}
2741static const struct wl_data_offer_listener data_offer_listener = {
2742 .offer = &offer,
2743 .source_actions = &source_actions,
2744 .action = &offer_action,
2745};
2747static void
2748data_offer(void *data, struct wl_data_device *wl_data_device,
2749 struct wl_data_offer *offer)
2750{
2751 struct seat *seat = data;
2752 data_offer_reset(&seat->clipboard);
2753 seat->clipboard.data_offer = offer;
2754 wl_data_offer_add_listener(offer, &data_offer_listener, seat);
2755}
2757static void
2758enter(void *data, struct wl_data_device *wl_data_device, uint32_t serial,
2759 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y,
2760 struct wl_data_offer *offer)
2761{
2762 struct seat *seat = data;
2763 struct wayland *wayl = seat->wayl;
2765 xassert(offer == seat->clipboard.data_offer);
2767 if (seat->clipboard.mime_type == DATA_OFFER_MIME_UNSET)
2768 goto reject_offer;
2770 /* Remember _which_ terminal the current DnD offer is targeting */
2771 xassert(seat->clipboard.window == NULL);
2772 tll_foreach(wayl->terms, it) {
2773 if (term_surface_kind(it->item, surface) == TERM_SURF_GRID &&
2774 !it->item->is_sending_paste_data)
2775 {
2776 wl_data_offer_accept(
2777 offer, serial, mime_type_map[seat->clipboard.mime_type]);
2778 wl_data_offer_set_actions(
2779 offer,
2780 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY,
2781 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY);
2783 seat->clipboard.window = it->item->window;
2784 return;
2785 }
2786 }
2788reject_offer:
2789 /* Either terminal is already busy sending paste data, or mouse
2790 * pointer isn't over the grid */
2791 seat->clipboard.window = NULL;
2792 wl_data_offer_accept(offer, serial, NULL);
2793 wl_data_offer_set_actions(
2794 offer,
2795 WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE,
2796 WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE);
2797}
2799static void
2800leave(void *data, struct wl_data_device *wl_data_device)
2801{
2802 struct seat *seat = data;
2803 seat->clipboard.window = NULL;
2804}
2806static void
2807motion(void *data, struct wl_data_device *wl_data_device, uint32_t time,
2808 wl_fixed_t x, wl_fixed_t y)
2809{
2810}
2812struct dnd_context {
2813 struct terminal *term;
2814 struct wl_data_offer *data_offer;
2815};
2817static void
2818receive_dnd(char *data, size_t size, void *user)
2819{
2820 struct dnd_context *ctx = user;
2821 receive_offer(data, size, ctx->term);
2822}
2824static void
2825receive_dnd_done(void *user)
2826{
2827 struct dnd_context *ctx = user;
2829 wl_data_offer_finish(ctx->data_offer);
2830 wl_data_offer_destroy(ctx->data_offer);
2831 receive_offer_done(ctx->term);
2832 free(ctx);
2833}
2835static void
2836drop(void *data, struct wl_data_device *wl_data_device)
2837{
2838 struct seat *seat = data;
2840 xassert(seat->clipboard.window != NULL);
2841 struct terminal *term = seat->clipboard.window->term;
2843 struct wl_clipboard *clipboard = &seat->clipboard;
2845 if (clipboard->mime_type == DATA_OFFER_MIME_UNSET) {
2846 LOG_WARN("compositor called data_device::drop() "
2847 "even though we rejected the drag-and-drop");
2848 return;
2849 }
2851 struct dnd_context *ctx = xmalloc(sizeof(*ctx));
2852 *ctx = (struct dnd_context){
2853 .term = term,
2854 .data_offer = clipboard->data_offer,
2855 };
2857 /* Prepare a pipe the other client can write its selection to us */
2858 int fds[2];
2859 if (pipe2(fds, O_CLOEXEC) == -1) {
2860 LOG_ERRNO("failed to create pipe");
2861 free(ctx);
2862 return;
2863 }
2865 int read_fd = fds[0];
2866 int write_fd = fds[1];
2868 LOG_DBG("DnD drop: mime-type=%s", mime_type_map[clipboard->mime_type]);
2870 /* Give write-end of pipe to other client */
2871 wl_data_offer_receive(
2872 clipboard->data_offer, mime_type_map[clipboard->mime_type], write_fd);
2874 /* Don't keep our copy of the write-end open (or we'll never get EOF) */
2875 close(write_fd);
2877 term->is_sending_paste_data = true;
2879 if (term->bracketed_paste)
2880 term_paste_data_to_slave(term, "\033[200~", 6);
2882 begin_receive_clipboard(
2883 term, false, read_fd, clipboard->mime_type,
2884 &receive_dnd, &receive_dnd_done, ctx);
2886 /* data offer is now "owned" by the receive context */
2887 clipboard->data_offer = NULL;
2888 clipboard->mime_type = DATA_OFFER_MIME_UNSET;
2889}
2891static void
2892selection(void *data, struct wl_data_device *wl_data_device,
2893 struct wl_data_offer *offer)
2894{
2895 /* Selection offer from other client */
2896 struct seat *seat = data;
2897 if (offer == NULL)
2898 data_offer_reset(&seat->clipboard);
2899 else
2900 xassert(offer == seat->clipboard.data_offer);
2901}
2903const struct wl_data_device_listener data_device_listener = {
2904 .data_offer = &data_offer,
2905 .enter = &enter,
2906 .leave = &leave,
2907 .motion = &motion,
2908 .drop = &drop,
2909 .selection = &selection,
2910};
2912static void
2913primary_offer(void *data,
2914 struct zwp_primary_selection_offer_v1 *zwp_primary_selection_offer,
2915 const char *mime_type)
2916{
2917 LOG_DBG("primary offer: %s", mime_type);
2918 struct seat *seat = data;
2919 select_mime_type_for_offer(mime_type, &seat->primary.mime_type);
2920}
2922static const struct zwp_primary_selection_offer_v1_listener primary_selection_offer_listener = {
2923 .offer = &primary_offer,
2924};
2926static void
2927primary_offer_reset(struct wl_primary *primary)
2928{
2929 if (primary->data_offer != NULL) {
2930 zwp_primary_selection_offer_v1_destroy(primary->data_offer);
2931 primary->data_offer = NULL;
2932 }
2934 primary->mime_type = DATA_OFFER_MIME_UNSET;
2935}
2937static void
2938primary_data_offer(void *data,
2939 struct zwp_primary_selection_device_v1 *zwp_primary_selection_device,
2940 struct zwp_primary_selection_offer_v1 *offer)
2941{
2942 struct seat *seat = data;
2943 primary_offer_reset(&seat->primary);
2944 seat->primary.data_offer = offer;
2945 zwp_primary_selection_offer_v1_add_listener(
2946 offer, &primary_selection_offer_listener, seat);
2947}
2949static void
2950primary_selection(void *data,
2951 struct zwp_primary_selection_device_v1 *zwp_primary_selection_device,
2952 struct zwp_primary_selection_offer_v1 *offer)
2953{
2954 /* Selection offer from other client, for primary */
2956 struct seat *seat = data;
2957 if (offer == NULL)
2958 primary_offer_reset(&seat->primary);
2959 else
2960 xassert(seat->primary.data_offer == offer);
2961}
2963const struct zwp_primary_selection_device_v1_listener primary_selection_device_listener = {
2964 .data_offer = &primary_data_offer,
2965 .selection = &primary_selection,
2966};
Diagnostics5 git calls · 1.1 s
CallStatusSizeTime01.1 s
discover200178 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=178 git.server.agent=git/2.56.1-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=178 git.pkt.payload.max=35
ls-refs20010 KiB467 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=427 git.response.bytes=10322 git.response.cache=EXPIRED git.response.duration_ms=467 git.protocol.version=2 git.pkt.frames=108 git.pkt.data=107 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=10322 git.pkt.payload.max=106 git.ls_refs.count=107 git.ls_refs.head=refs/heads/master
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=8830 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=8830 git.pkt.payload.max=8730 git.sideband.band1.bytes=8730 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=14 git.pack.bytes=8730 git.fetch.wide.objects=14 git.fetch.wide.extra_bytes=9909 git.fetch.wide.blobs=0
fetchtree:0 · deepen 102009.3 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=9489 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=9489 git.pkt.payload.max=9389 git.sideband.band1.bytes=9389 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=9389 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a git.pack.full=10Open pack map
fetch20016 KiB659 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=659 git.response.bytes=16203 git.response.cache=MISS git.response.duration_ms=659 git.fetch.wants=c17a96404c87ff64061266df5b6590e30f5959b0 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=16203 git.pkt.payload.max=16176 git.sideband.band1.bytes=16176 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://codeberg.org/dnkl/foot
- Ref
- refs/heads/master
- Commit
- c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
- cf-ray
- a5e2175359ffecd4
- Request
- 255997be…
- Protocol
- v2 · sha1
- Agent
- git/2.56.1-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format