1 |
|
|
/* $OpenBSD: screen.c,v 1.47 2017/06/04 09:02:36 nicm Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> |
5 |
|
|
* |
6 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
7 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
8 |
|
|
* copyright notice and this permission notice appear in all copies. |
9 |
|
|
* |
10 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER |
15 |
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
16 |
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include <sys/types.h> |
20 |
|
|
|
21 |
|
|
#include <stdlib.h> |
22 |
|
|
#include <string.h> |
23 |
|
|
#include <unistd.h> |
24 |
|
|
#include <vis.h> |
25 |
|
|
|
26 |
|
|
#include "tmux.h" |
27 |
|
|
|
28 |
|
|
static void screen_resize_x(struct screen *, u_int); |
29 |
|
|
static void screen_resize_y(struct screen *, u_int); |
30 |
|
|
|
31 |
|
|
static void screen_reflow(struct screen *, u_int); |
32 |
|
|
|
33 |
|
|
/* Create a new screen. */ |
34 |
|
|
void |
35 |
|
|
screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit) |
36 |
|
|
{ |
37 |
|
|
s->grid = grid_create(sx, sy, hlimit); |
38 |
|
|
s->title = xstrdup(""); |
39 |
|
|
|
40 |
|
|
s->cstyle = 0; |
41 |
|
|
s->ccolour = xstrdup(""); |
42 |
|
|
s->tabs = NULL; |
43 |
|
|
|
44 |
|
|
screen_reinit(s); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
/* Reinitialise screen. */ |
48 |
|
|
void |
49 |
|
|
screen_reinit(struct screen *s) |
50 |
|
|
{ |
51 |
|
|
s->cx = 0; |
52 |
|
|
s->cy = 0; |
53 |
|
|
|
54 |
|
|
s->rupper = 0; |
55 |
|
|
s->rlower = screen_size_y(s) - 1; |
56 |
|
|
|
57 |
|
|
s->mode = MODE_CURSOR | MODE_WRAP; |
58 |
|
|
|
59 |
|
|
screen_reset_tabs(s); |
60 |
|
|
|
61 |
|
|
grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8); |
62 |
|
|
|
63 |
|
|
screen_clear_selection(s); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
/* Destroy a screen. */ |
67 |
|
|
void |
68 |
|
|
screen_free(struct screen *s) |
69 |
|
|
{ |
70 |
|
|
free(s->tabs); |
71 |
|
|
free(s->title); |
72 |
|
|
free(s->ccolour); |
73 |
|
|
grid_destroy(s->grid); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
/* Reset tabs to default, eight spaces apart. */ |
77 |
|
|
void |
78 |
|
|
screen_reset_tabs(struct screen *s) |
79 |
|
|
{ |
80 |
|
|
u_int i; |
81 |
|
|
|
82 |
|
|
free(s->tabs); |
83 |
|
|
|
84 |
|
|
if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL) |
85 |
|
|
fatal("bit_alloc failed"); |
86 |
|
|
for (i = 8; i < screen_size_x(s); i += 8) |
87 |
|
|
bit_set(s->tabs, i); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
/* Set screen cursor style. */ |
91 |
|
|
void |
92 |
|
|
screen_set_cursor_style(struct screen *s, u_int style) |
93 |
|
|
{ |
94 |
|
|
if (style <= 6) |
95 |
|
|
s->cstyle = style; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
/* Set screen cursor colour. */ |
99 |
|
|
void |
100 |
|
|
screen_set_cursor_colour(struct screen *s, const char *colour) |
101 |
|
|
{ |
102 |
|
|
free(s->ccolour); |
103 |
|
|
s->ccolour = xstrdup(colour); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
/* Set screen title. */ |
107 |
|
|
void |
108 |
|
|
screen_set_title(struct screen *s, const char *title) |
109 |
|
|
{ |
110 |
|
|
free(s->title); |
111 |
|
|
utf8_stravis(&s->title, title, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
/* Resize screen. */ |
115 |
|
|
void |
116 |
|
|
screen_resize(struct screen *s, u_int sx, u_int sy, int reflow) |
117 |
|
|
{ |
118 |
|
|
if (sx < 1) |
119 |
|
|
sx = 1; |
120 |
|
|
if (sy < 1) |
121 |
|
|
sy = 1; |
122 |
|
|
|
123 |
|
|
if (sx != screen_size_x(s)) { |
124 |
|
|
screen_resize_x(s, sx); |
125 |
|
|
|
126 |
|
|
/* |
127 |
|
|
* It is unclear what should happen to tabs on resize. xterm |
128 |
|
|
* seems to try and maintain them, rxvt resets them. Resetting |
129 |
|
|
* is simpler and more reliable so let's do that. |
130 |
|
|
*/ |
131 |
|
|
screen_reset_tabs(s); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
if (sy != screen_size_y(s)) |
135 |
|
|
screen_resize_y(s, sy); |
136 |
|
|
|
137 |
|
|
if (reflow) |
138 |
|
|
screen_reflow(s, sx); |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
static void |
142 |
|
|
screen_resize_x(struct screen *s, u_int sx) |
143 |
|
|
{ |
144 |
|
|
struct grid *gd = s->grid; |
145 |
|
|
|
146 |
|
|
if (sx == 0) |
147 |
|
|
fatalx("zero size"); |
148 |
|
|
|
149 |
|
|
/* |
150 |
|
|
* Treat resizing horizontally simply: just ensure the cursor is |
151 |
|
|
* on-screen and change the size. Don't bother to truncate any lines - |
152 |
|
|
* then the data should be accessible if the size is then increased. |
153 |
|
|
* |
154 |
|
|
* The only potential wrinkle is if UTF-8 double-width characters are |
155 |
|
|
* left in the last column, but UTF-8 terminals should deal with this |
156 |
|
|
* sanely. |
157 |
|
|
*/ |
158 |
|
|
if (s->cx >= sx) |
159 |
|
|
s->cx = sx - 1; |
160 |
|
|
gd->sx = sx; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
static void |
164 |
|
|
screen_resize_y(struct screen *s, u_int sy) |
165 |
|
|
{ |
166 |
|
|
struct grid *gd = s->grid; |
167 |
|
|
u_int needed, available, oldy, i; |
168 |
|
|
|
169 |
|
|
if (sy == 0) |
170 |
|
|
fatalx("zero size"); |
171 |
|
|
oldy = screen_size_y(s); |
172 |
|
|
|
173 |
|
|
/* |
174 |
|
|
* When resizing: |
175 |
|
|
* |
176 |
|
|
* If the height is decreasing, delete lines from the bottom until |
177 |
|
|
* hitting the cursor, then push lines from the top into the history. |
178 |
|
|
* |
179 |
|
|
* When increasing, pull as many lines as possible from scrolled |
180 |
|
|
* history (not explicitly cleared from view) to the top, then fill the |
181 |
|
|
* remaining with blanks at the bottom. |
182 |
|
|
*/ |
183 |
|
|
|
184 |
|
|
/* Size decreasing. */ |
185 |
|
|
if (sy < oldy) { |
186 |
|
|
needed = oldy - sy; |
187 |
|
|
|
188 |
|
|
/* Delete as many lines as possible from the bottom. */ |
189 |
|
|
available = oldy - 1 - s->cy; |
190 |
|
|
if (available > 0) { |
191 |
|
|
if (available > needed) |
192 |
|
|
available = needed; |
193 |
|
|
grid_view_delete_lines(gd, oldy - available, available, |
194 |
|
|
8); |
195 |
|
|
} |
196 |
|
|
needed -= available; |
197 |
|
|
|
198 |
|
|
/* |
199 |
|
|
* Now just increase the history size, if possible, to take |
200 |
|
|
* over the lines which are left. If history is off, delete |
201 |
|
|
* lines from the top. |
202 |
|
|
*/ |
203 |
|
|
available = s->cy; |
204 |
|
|
if (gd->flags & GRID_HISTORY) { |
205 |
|
|
gd->hscrolled += needed; |
206 |
|
|
gd->hsize += needed; |
207 |
|
|
} else if (needed > 0 && available > 0) { |
208 |
|
|
if (available > needed) |
209 |
|
|
available = needed; |
210 |
|
|
grid_view_delete_lines(gd, 0, available, 8); |
211 |
|
|
} |
212 |
|
|
s->cy -= needed; |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
/* Resize line arrays. */ |
216 |
|
|
gd->linedata = xreallocarray(gd->linedata, gd->hsize + sy, |
217 |
|
|
sizeof *gd->linedata); |
218 |
|
|
|
219 |
|
|
/* Size increasing. */ |
220 |
|
|
if (sy > oldy) { |
221 |
|
|
needed = sy - oldy; |
222 |
|
|
|
223 |
|
|
/* |
224 |
|
|
* Try to pull as much as possible out of scrolled history, if |
225 |
|
|
* is is enabled. |
226 |
|
|
*/ |
227 |
|
|
available = gd->hscrolled; |
228 |
|
|
if (gd->flags & GRID_HISTORY && available > 0) { |
229 |
|
|
if (available > needed) |
230 |
|
|
available = needed; |
231 |
|
|
gd->hscrolled -= available; |
232 |
|
|
gd->hsize -= available; |
233 |
|
|
s->cy += available; |
234 |
|
|
} else |
235 |
|
|
available = 0; |
236 |
|
|
needed -= available; |
237 |
|
|
|
238 |
|
|
/* Then fill the rest in with blanks. */ |
239 |
|
|
for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++) |
240 |
|
|
memset(&gd->linedata[i], 0, sizeof gd->linedata[i]); |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
/* Set the new size, and reset the scroll region. */ |
244 |
|
|
gd->sy = sy; |
245 |
|
|
s->rupper = 0; |
246 |
|
|
s->rlower = screen_size_y(s) - 1; |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
/* Set selection. */ |
250 |
|
|
void |
251 |
|
|
screen_set_selection(struct screen *s, u_int sx, u_int sy, |
252 |
|
|
u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc) |
253 |
|
|
{ |
254 |
|
|
struct screen_sel *sel = &s->sel; |
255 |
|
|
|
256 |
|
|
memcpy(&sel->cell, gc, sizeof sel->cell); |
257 |
|
|
sel->flag = 1; |
258 |
|
|
sel->hidden = 0; |
259 |
|
|
|
260 |
|
|
sel->rectflag = rectflag; |
261 |
|
|
|
262 |
|
|
sel->sx = sx; sel->sy = sy; |
263 |
|
|
sel->ex = ex; sel->ey = ey; |
264 |
|
|
} |
265 |
|
|
|
266 |
|
|
/* Clear selection. */ |
267 |
|
|
void |
268 |
|
|
screen_clear_selection(struct screen *s) |
269 |
|
|
{ |
270 |
|
|
struct screen_sel *sel = &s->sel; |
271 |
|
|
|
272 |
|
|
sel->flag = 0; |
273 |
|
|
sel->hidden = 0; |
274 |
|
|
sel->lineflag = LINE_SEL_NONE; |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
/* Hide selection. */ |
278 |
|
|
void |
279 |
|
|
screen_hide_selection(struct screen *s) |
280 |
|
|
{ |
281 |
|
|
struct screen_sel *sel = &s->sel; |
282 |
|
|
|
283 |
|
|
sel->hidden = 1; |
284 |
|
|
} |
285 |
|
|
|
286 |
|
|
/* Check if cell in selection. */ |
287 |
|
|
int |
288 |
|
|
screen_check_selection(struct screen *s, u_int px, u_int py) |
289 |
|
|
{ |
290 |
|
|
struct screen_sel *sel = &s->sel; |
291 |
|
|
u_int xx; |
292 |
|
|
|
293 |
|
|
if (!sel->flag || sel->hidden) |
294 |
|
|
return (0); |
295 |
|
|
|
296 |
|
|
if (sel->rectflag) { |
297 |
|
|
if (sel->sy < sel->ey) { |
298 |
|
|
/* start line < end line -- downward selection. */ |
299 |
|
|
if (py < sel->sy || py > sel->ey) |
300 |
|
|
return (0); |
301 |
|
|
} else if (sel->sy > sel->ey) { |
302 |
|
|
/* start line > end line -- upward selection. */ |
303 |
|
|
if (py > sel->sy || py < sel->ey) |
304 |
|
|
return (0); |
305 |
|
|
} else { |
306 |
|
|
/* starting line == ending line. */ |
307 |
|
|
if (py != sel->sy) |
308 |
|
|
return (0); |
309 |
|
|
} |
310 |
|
|
|
311 |
|
|
/* |
312 |
|
|
* Need to include the selection start row, but not the cursor |
313 |
|
|
* row, which means the selection changes depending on which |
314 |
|
|
* one is on the left. |
315 |
|
|
*/ |
316 |
|
|
if (sel->ex < sel->sx) { |
317 |
|
|
/* Cursor (ex) is on the left. */ |
318 |
|
|
if (px < sel->ex) |
319 |
|
|
return (0); |
320 |
|
|
|
321 |
|
|
if (px > sel->sx) |
322 |
|
|
return (0); |
323 |
|
|
} else { |
324 |
|
|
/* Selection start (sx) is on the left. */ |
325 |
|
|
if (px < sel->sx) |
326 |
|
|
return (0); |
327 |
|
|
|
328 |
|
|
if (px > sel->ex) |
329 |
|
|
return (0); |
330 |
|
|
} |
331 |
|
|
} else { |
332 |
|
|
/* |
333 |
|
|
* Like emacs, keep the top-left-most character, and drop the |
334 |
|
|
* bottom-right-most, regardless of copy direction. |
335 |
|
|
*/ |
336 |
|
|
if (sel->sy < sel->ey) { |
337 |
|
|
/* starting line < ending line -- downward selection. */ |
338 |
|
|
if (py < sel->sy || py > sel->ey) |
339 |
|
|
return (0); |
340 |
|
|
|
341 |
|
|
if (py == sel->sy && px < sel->sx) |
342 |
|
|
return (0); |
343 |
|
|
|
344 |
|
|
if (py == sel->ey && px > sel->ex) |
345 |
|
|
return (0); |
346 |
|
|
} else if (sel->sy > sel->ey) { |
347 |
|
|
/* starting line > ending line -- upward selection. */ |
348 |
|
|
if (py > sel->sy || py < sel->ey) |
349 |
|
|
return (0); |
350 |
|
|
|
351 |
|
|
if (py == sel->ey && px < sel->ex) |
352 |
|
|
return (0); |
353 |
|
|
|
354 |
|
|
if (sel->modekeys == MODEKEY_EMACS) |
355 |
|
|
xx = sel->sx - 1; |
356 |
|
|
else |
357 |
|
|
xx = sel->sx; |
358 |
|
|
if (py == sel->sy && (sel->sx == 0 || px > xx)) |
359 |
|
|
return (0); |
360 |
|
|
} else { |
361 |
|
|
/* starting line == ending line. */ |
362 |
|
|
if (py != sel->sy) |
363 |
|
|
return (0); |
364 |
|
|
|
365 |
|
|
if (sel->ex < sel->sx) { |
366 |
|
|
/* cursor (ex) is on the left */ |
367 |
|
|
if (sel->modekeys == MODEKEY_EMACS) |
368 |
|
|
xx = sel->sx - 1; |
369 |
|
|
else |
370 |
|
|
xx = sel->sx; |
371 |
|
|
if (px > xx || px < sel->ex) |
372 |
|
|
return (0); |
373 |
|
|
} else { |
374 |
|
|
/* selection start (sx) is on the left */ |
375 |
|
|
if (px < sel->sx || px > sel->ex) |
376 |
|
|
return (0); |
377 |
|
|
} |
378 |
|
|
} |
379 |
|
|
} |
380 |
|
|
|
381 |
|
|
return (1); |
382 |
|
|
} |
383 |
|
|
|
384 |
|
|
/* Get selected grid cell. */ |
385 |
|
|
void |
386 |
|
|
screen_select_cell(struct screen *s, struct grid_cell *dst, |
387 |
|
|
const struct grid_cell *src) |
388 |
|
|
{ |
389 |
|
|
if (!s->sel.flag || s->sel.hidden) |
390 |
|
|
return; |
391 |
|
|
|
392 |
|
|
memcpy(dst, &s->sel.cell, sizeof *dst); |
393 |
|
|
|
394 |
|
|
utf8_copy(&dst->data, &src->data); |
395 |
|
|
dst->attr = dst->attr & ~GRID_ATTR_CHARSET; |
396 |
|
|
dst->attr |= src->attr & GRID_ATTR_CHARSET; |
397 |
|
|
dst->flags = src->flags; |
398 |
|
|
} |
399 |
|
|
|
400 |
|
|
/* Reflow wrapped lines. */ |
401 |
|
|
static void |
402 |
|
|
screen_reflow(struct screen *s, u_int new_x) |
403 |
|
|
{ |
404 |
|
|
struct grid *old = s->grid; |
405 |
|
|
u_int change; |
406 |
|
|
|
407 |
|
|
s->grid = grid_create(old->sx, old->sy, old->hlimit); |
408 |
|
|
|
409 |
|
|
change = grid_reflow(s->grid, old, new_x); |
410 |
|
|
if (change < s->cy) |
411 |
|
|
s->cy -= change; |
412 |
|
|
else |
413 |
|
|
s->cy = 0; |
414 |
|
|
} |