1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
#include "selection.h"
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define LOG_MODULE "selection"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "render.h"
#include "grid.h"
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
static bool
selection_enabled(const struct terminal *term)
{
return term->mouse_tracking == MOUSE_NONE;
}
static char *
extract_selection(const struct terminal *term)
{
const struct coord *start = &term->selection.start;
const struct coord *end = &term->selection.end;
if (start->row > end->row || (start->row == end->row && start->col > end->col)) {
const struct coord *tmp = start;
start = end;
end = tmp;
}
assert(start->row <= end->row);
size_t max_cells = 0;
if (start->row == end->row) {
assert(start->col <= end->col);
max_cells = end->col - start->col + 1;
} else {
max_cells = term->cols - start->col;
max_cells += term->cols * (end->row - start->row - 1);
max_cells += end->col + 1;
}
char *buf = malloc(max_cells * 4 + 1);
int idx = 0;
int start_col = start->col;
for (int r = start->row; r < end->row; r++) {
const struct row *row = grid_row_in_view(term->grid, r - term->grid->view);
if (row == NULL)
continue;
/* TODO: replace '\0' with spaces, then trim lines */
for (int col = start_col; col < term->cols; col++) {
const struct cell *cell = &row->cells[col];
if (cell->c[0] == '\0')
continue;
size_t len = strnlen(cell->c, 4);
memcpy(&buf[idx], cell->c, len);
idx += len;
}
if (r != end->row - 1)
buf[idx++] = '\n';
start_col = 0;
}
{
const struct row *row = grid_row_in_view(term->grid, end->row - term->grid->view);
for (int col = start_col; row != NULL && col <= end->col; col++) {
const struct cell *cell = &row->cells[col];
if (cell->c[0] == '\0')
continue;
size_t len = strnlen(cell->c, 4);
memcpy(&buf[idx], cell->c, len);
idx += len;
}
}
buf[idx] = '\0';
return buf;
}
void
selection_start(struct terminal *term, int col, int row)
{
if (!selection_enabled(term))
return;
selection_cancel(term);
LOG_DBG("selection started at %d,%d", row, col);
term->selection.start = (struct coord){col, row};
term->selection.end = (struct coord){-1, -1};
}
void
selection_update(struct terminal *term, int col, int row)
{
if (!selection_enabled(term))
return;
LOG_DBG("selection updated: start = %d,%d, end = %d,%d -> %d, %d",
term->selection.start.row, term->selection.start.col,
term->selection.end.row, term->selection.end.col,
row, col);
int start_row = term->selection.start.row;
int old_end_row = term->selection.end.row;
int new_end_row = term->grid->view + row;
assert(start_row != -1);
assert(new_end_row != -1);
if (old_end_row == -1)
old_end_row = new_end_row;
int from = min(start_row, min(old_end_row, new_end_row));
int to = max(start_row, max(old_end_row, new_end_row));
term->selection.end = (struct coord){col, term->grid->view + row};
assert(term->selection.start.row != -1 && term->selection.end.row != -1);
term_damage_rows_in_view(term, from - term->grid->view, to - term->grid->view);
if (term->frame_callback == NULL)
grid_render(term);
}
void
selection_finalize(struct terminal *term)
{
if (!selection_enabled(term))
return;
if (term->selection.start.row == -1 || term->selection.end.row == -1)
return;
assert(term->selection.start.row != -1);
assert(term->selection.end.row != -1);
/* Kill old primary selection */
free(term->selection.primary.text);
/* TODO: primary data source */
term->selection.primary.text = extract_selection(term);
}
void
selection_cancel(struct terminal *term)
{
if (!selection_enabled(term))
return;
LOG_DBG("selection cancelled: start = %d,%d end = %d,%d",
term->selection.start.row, term->selection.start.col,
term->selection.end.row, term->selection.end.col);
int start_row = term->selection.start.row;
int end_row = term->selection.end.row;
term->selection.start = (struct coord){-1, -1};
term->selection.end = (struct coord){-1, -1};
if (start_row != -1 && end_row != -1) {
term_damage_rows_in_view(
term,
min(start_row, end_row) - term->grid->view,
max(start_row, end_row) - term->grid->view);
if (term->frame_callback == NULL)
grid_render(term);
}
}
static void
target(void *data, struct wl_data_source *wl_data_source, const char *mime_type)
{
LOG_WARN("TARGET: mime-type=%s", mime_type);
}
static void
send(void *data, struct wl_data_source *wl_data_source, const char *mime_type,
int32_t fd)
{
const struct clipboard *clipboard
= wl_data_source_get_user_data(wl_data_source);
assert(clipboard != NULL);
assert(clipboard->text != NULL);
size_t left = strlen(clipboard->text);
size_t idx = 0;
while (left > 0) {
ssize_t ret = write(fd, &clipboard->text[idx], left);
if (ret == -1 && errno != EINTR) {
LOG_ERRNO("failed to write to clipboard");
break;
}
left -= ret;
idx += ret;
}
close(fd);
}
static void
cancelled(void *data, struct wl_data_source *wl_data_source)
{
struct clipboard *clipboard = wl_data_source_get_user_data(wl_data_source);
assert(clipboard->data_source == wl_data_source);
wl_data_source_destroy(clipboard->data_source);
clipboard->data_source = NULL;
clipboard->serial = 0;
free(clipboard->text);
clipboard->text = NULL;
}
static void
dnd_drop_performed(void *data, struct wl_data_source *wl_data_source)
{
}
static void
dnd_finished(void *data, struct wl_data_source *wl_data_source)
{
}
static void
action(void *data, struct wl_data_source *wl_data_source, uint32_t dnd_action)
{
}
static const struct wl_data_source_listener data_source_listener = {
.target = &target,
.send = &send,
.cancelled = &cancelled,
.dnd_drop_performed = &dnd_drop_performed,
.dnd_finished = &dnd_finished,
.action = &action,
};
void
selection_to_clipboard(struct terminal *term, uint32_t serial)
{
if (term->selection.clipboard.data_source != NULL) {
/* Kill previous data source */
struct clipboard *clipboard = &term->selection.clipboard;
assert(clipboard->serial != 0);
wl_data_device_set_selection(term->wl.data_device, NULL, clipboard->serial);
wl_data_source_destroy(clipboard->data_source);
free(clipboard->text);
clipboard->data_source = NULL;
clipboard->serial = 0;
}
struct clipboard *clipboard = &term->selection.clipboard;
/* Get selection as a string */
clipboard->text = extract_selection(term);
clipboard->data_source
= wl_data_device_manager_create_data_source(term->wl.data_device_manager);
if (clipboard->data_source == NULL) {
LOG_ERR("failed to create clipboard data source");
return;
}
/* Configure source */
wl_data_source_offer(clipboard->data_source, "text/plain;charset=utf-8");
wl_data_source_add_listener(clipboard->data_source, &data_source_listener, term);
wl_data_device_set_selection(term->wl.data_device, clipboard->data_source, serial);
wl_data_source_set_user_data(clipboard->data_source, clipboard);
/* Needed when sending the selection to other client */
clipboard->serial = serial;
}
void
selection_from_clipboard(struct terminal *term, uint32_t serial)
{
struct clipboard *clipboard = &term->selection.clipboard;
if (clipboard->data_offer == NULL)
return;
/* Prepare a pipe the other client can write its selection to us */
int fds[2];
if (pipe2(fds, O_CLOEXEC) == -1) {
LOG_ERRNO("failed to create pipe");
return;
}
int read_fd = fds[0];
int write_fd = fds[1];
/* Give write-end of pipe to other client */
wl_data_offer_receive(
clipboard->data_offer, "text/plain;charset=utf-8", write_fd);
wl_display_roundtrip(term->wl.display);
/* Don't keep our copy of the write-end open (or we'll never get EOF) */
close(write_fd);
if (term->bracketed_paste)
write(term->ptmx, "\033[200~", 6);
/* Read until EOF */
while (true) {
char text[256];
ssize_t amount = read(read_fd, text, sizeof(text));
if (amount == -1) {
LOG_ERRNO("failed to read clipboard data: %d", errno);
break;
} else if (amount == 0)
break;
write(term->ptmx, text, amount);
}
if (term->bracketed_paste)
write(term->ptmx, "\033[201~", 6);
close(read_fd);
}
void
selection_from_primary(struct terminal *term)
{
LOG_WARN("selection from PRIMARY");
}
static void
offer(void *data, struct wl_data_offer *wl_data_offer, const char *mime_type)
{
}
static void
source_actions(void *data, struct wl_data_offer *wl_data_offer,
uint32_t source_actions)
{
}
static void
offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
{
}
static const struct wl_data_offer_listener data_offer_listener = {
.offer = &offer,
.source_actions = &source_actions,
.action = &offer_action,
};
static void
data_offer(void *data, struct wl_data_device *wl_data_device,
struct wl_data_offer *id)
{
}
static void
enter(void *data, struct wl_data_device *wl_data_device, uint32_t serial,
struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y,
struct wl_data_offer *id)
{
}
static void
leave(void *data, struct wl_data_device *wl_data_device)
{
}
static void
motion(void *data, struct wl_data_device *wl_data_device, uint32_t time,
wl_fixed_t x, wl_fixed_t y)
{
}
static void
drop(void *data, struct wl_data_device *wl_data_device)
{
}
static void
selection(void *data, struct wl_data_device *wl_data_device,
struct wl_data_offer *id)
{
/* Selection offer from other client */
struct terminal *term = data;
struct clipboard *clipboard = &term->selection.clipboard;
if (clipboard->data_offer != NULL)
wl_data_offer_destroy(clipboard->data_offer);
clipboard->data_offer = id;
if (id != NULL)
wl_data_offer_add_listener(id, &data_offer_listener, term);
}
const struct wl_data_device_listener data_device_listener = {
.data_offer = &data_offer,
.enter = &enter,
.leave = &leave,
.motion = &motion,
.drop = &drop,
.selection = &selection,
};
|